閱讀英文原版和看翻譯是不一樣的,看英文更容易理解其中的內涵。打算利用空餘時間閱讀官方的Training,並記錄下筆記和自己的一些思考。如有理解有誤的地方,歡迎指正。
前兩節基本是介紹,不作筆記了。直接從生命週期開始記錄。
先看看官方對android生命週期的一個宏觀介紹:
Unlike other programming paradigms in which apps are launched with a main()
method,
the Android system initiates code in an Activity
instance
by invoking specific callback methods that correspond to specific stages of its lifecycle. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity.
介紹了android程序與其他程序不一樣的地方。
生命週期流程圖,google已改爲金字塔形式:
對插圖的介紹:
Figure 1. A simplified illustration of the Activity lifecycle, expressed as a step pyramid. This shows how, for every callback used to take the activity a step toward the Resumed state at the top, there's a callback method that takes the activity a step
down. The activity can also return to the resumed state from the Paused and Stopped state.
1.Does not crash if the user receives a phone call or switches to another app while using your app.
2.Does not consume valuable system resources when the user is not actively using it.
3.Does not lose the user's progress if they leave your app and return to it at a later time.
4.Does not crash or lose the user's progress when the screen rotates between landscape and portrait orientation.
However, only three of these states can be static. That is, the activity can exist in one of only three states for an extended period of time:
Resumed
In this state, the activity is in the foreground and the user can interact with it. (Also sometimes referred to as the "running" state.)
Paused
In this state, the activity is partially obscured by another activity—the other activity that's in the foreground is semi-transparent or doesn't cover the entire screen. The paused activity does not receive user input and cannot execute any code.
Stopped
In this state, the activity is completely hidden and not visible to the user; it is considered to be in the background. While stopped, the activity instance and all its state information such as member variables is retained, but it cannot execute any code.
If either the MAIN action or LAUNCHER category are not declared for one of your activities, then your app icon will not appear in the Home screen's list of apps.
沒有聲明入口Activity,手機則不會出現app的圖標:
..省略..the system creates every new instance of Activity by calling its onCreate() method.
You must implement the onCreate() method to perform basic application startup logic that should happen only once for the entire life of the activity. For example, your implementation of onCreate() should define the user interface and possibly instantiate some
class-scope variables.
所有的Activity都是通過onCreate()方法實例化的。
Once the onCreate() finishes execution, the system calls the onStart() and onResume() methods in quick succession. Your activity never resides in the Created or Started states. Technically, the activity becomes visible to the user when onStart() is called, but onResume() quickly follows and the activity remains in the Resumed state until something occurs to change that, such as when a phone call is received, the user navigates to another activity, or the device screen turns off.
應用不會再onCreate()和onStart()中駐留,都是快速執行過去的。從onStart()開始即可見,只不過時間很短暫即進入onResume()階段。
While the activity's first lifecycle callback is onCreate(), its very last callback is onDestroy(). The system calls this method on your activity as the final signal that your activity instance is being completely removed from the system memory.
Most apps don't need to implement this method because local class references are destroyed with the activity and your activity should perform most cleanup during onPause() and onStop(). However, if your activity includes background threads that you created
during onCreate() or other long-running resources that could potentially leak memory if not properly closed, you should kill them during onDestroy().
The system calls onDestroy() after it has already called onPause() and onStop() in all situations except one: when you call finish() from within the onCreate() method. In some cases, such as when your activity operates as a temporary decision maker to launch
another activity, you might call finish() from within onCreate() to destroy the activity. In this case, the system immediately calls onDestroy() without calling any of the other lifecycle methods.