Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 官方文件翻譯(十六):Managing the Activity Lifecycle 管理 Activity 生命週期

編輯:關於 Android 程式設計

Managing the Activity Lifecycle

管理 Activity 的生命週期

Dependencies and prerequisites

依賴關係與先決條件

How to create an Android project (see Creating an Android Project)

如何建立一個 Android 專案(參閱 Creating an Android Project)

You should also read

你還需要閱讀

Activities


As a user navigates through, out of, and back to your app, the Activity instances in your app transition between different states in their lifecycle. For instance, when your activity starts for the first time, it comes to the foreground of the system and receives user focus. During this process, the Android system calls a series of lifecycle methods on the activity in which you set up the user interface and other components. If the user performs an action that starts another activity or switches to another app, the system calls another set of lifecycle methods on your activity as it moves into the background (where the activity is no longer visible, but the instance and its state remains intact).

當使用者在你的應用中穿梭、離開再回來,各個 Activity 實例也會在生命週期裡不斷轉換狀態。舉例來說,當 Activity 第一次啟動時,它會來到系統前台並取得使用者焦點。這個過程中,Android 系統會依序呼叫 Activity 上的一系列生命週期方法,而你正是在這些方法裡設定使用者介面與其他元件。若使用者執行某個動作開啟了另一個 Activity,或切換到其他應用,系統會在你這邊進入背景時呼叫另一組生命週期方法——此時 Activity 已不可見,但實例本身與它的狀態依然完整保留。

Within the lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity. For example, if you're building a streaming video player, you might pause the video and terminate the network connection when the user switches to another app. When the user returns, you can reconnect to the network and allow the user to resume the video from the same spot.

在這些生命週期回呼方法中,你可以宣告 Activity 在使用者離開與再次進入時該如何反應。舉例來說,如果你正在做一個串流播放器,當使用者切換到其他應用時,你可能會暫停播放並斷開網路連線;等使用者回來,再重新連線並讓影片從原本的位置續播。

This class explains important lifecycle callback methods that each Activity instance receives and how you can use them so your activity does what the user expects and does not consume system resources when your activity doesn't need them.

本課程說明每個 Activity 實例都會收到的重要生命週期回呼方法,以及該如何運用它們,讓 Activity 做出使用者期待的行為,並且在不需要時不白白消耗系統資源。

Lessons

課程大綱

Starting an Activity

啟動一個 Activity

Learn the basics about the activity lifecycle, how the user can launch your app, and how to perform basic activity creation.

學習生命週期的基礎知識、使用者如何啟動你的應用,以及如何完成最基本的 Activity 建立。

Pausing and Resuming an Activity

暫停與恢復一個 Activity

Learn what happens when your activity is paused (partially obscured) and resumed and what you should do during these state changes.

了解當 Activity 被暫停(部分被遮擋)與恢復時會發生什麼,以及在這些狀態轉換期間你應該做哪些事。

Stopping and Restarting an Activity

停止與重啟一個 Activity

Learn what happens when the user completely leaves your activity and returns to it.

了解當使用者完全離開你的 Activity,之後又回來時,系統做了什麼。

Recreating an Activity

重新建立一個 Activity

Learn what happens when your activity is destroyed and how you can rebuild the activity state when necessary.

了解當 Activity 被銷毀時會發生什麼,以及在必要時你該如何重建 Activity 的狀態。

Get started

開始動手


譯者補充:這四節課其實對應了 Activity 的四段關鍵旅程。實務上最容易出問題的是最後兩段——使用者在按鍵盤以外的情境下離開(例如來電、記憶體不足被回收),此時 onSaveInstanceState() 有沒有把該存的狀態存好,直接決定了使用者回來時看到的是「原本的畫面」還是「重來一次的空白頁」。而螢幕旋轉造成的 Activity 重建,也正是同一套機制在起作用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved