Activity Stacks
The state of each Activity is determined by its position on the Activity stack, a last-in–fi rst-out collection
of all the currently running Activities. When a new Activity starts, the current foreground screen is
moved to the top of the stack. If the user navigates back using the Back button, or the foreground Activity
is closed, the next Activity on the stack moves up and becomes active.This process is illustrated in
Figure 3-7.
As described previously in this chapter, an application’s priority is infl uenced by its highest-priority
Activity. The Android memory manager uses this stack to determine the priority of applications based
on their Activities when deciding which application to terminate to free resources.
Activity States
There are three nested loops that you can monitor by implementing them:
- The entire lifetime
of an activity happens between the first call
to
through to a single final call toonCreate()
. An activity does all its initial setup of "global" state inonDestroy()
onCreate()
, and releases all remaining resources inonDestroy()
. For example, if it has a thread running in the background to download data from the network, it may create that thread inonCreate()
and then stop the thread inonDestroy()
. -
The visible lifetime of an activity happens between a call to
until a corresponding call toonStart()
. During this time, the user can see the activity on-screen, though it may not be in the foreground and interacting with the user. Between these two methods, you can maintain resources that are needed to show the activity to the user. For example, you can register aonStop()
BroadcastReceiver
inonStart()
to monitor for changes that impact your UI, and unregister it inonStop()
when the user can no longer see what you are displaying. TheonStart()
andonStop()
methods can be called multiple times, as the activity alternates between being visible and hidden to the user. -
The foreground lifetime of an activity happens between a call to
until a corresponding call toonResume()
. During this time, the activity is in front of all other activities on screen and is interacting with the user. An activity can frequently transition between the resumed and paused states — for example,onPause()
onPause()
is called when the device goes to sleep or when a new activity is started,onResume()
is called when an activity result or a new intent is delivered. Therefore, the code in these two methods should be fairly lightweight.
The following diagram illustrates these loops and the paths an activity may take between states. The colored ovals are major states the activity can be in. The square rectangles represent the callback methods you can implement to perform operations when the activity transitions between states.
Three methods (onPause()
,
onStop()
, and onDestroy()
) are Killable.
Because
onPause()
is the first of the three, it's the only one that's
guaranteed to be called before the process is killed —
onStop()
and onDestroy()
may not be. Therefore, you
should use onPause()
to write any persistent data (such as user
edits) to storage.
Test Sample code:
The main Activity UI code of
ExampleActivity.java
The Second Activity UI code of
SecondActivity.java
Test Data:
When the program launch:
12-01 11:17:20.368: VERBOSE/Activity Lifecycle Main(353): onCreate
12-01 11:17:20.399: VERBOSE/Activity Lifecycle Main(353): onStart
12-01 11:17:20.410: VERBOSE/Activity Lifecycle Main(353): onResume
Then click the button to active the Second Activity:
ExampleActivity Log:
12-01 11:20:41.379: VERBOSE/Activity Lifecycle Main(353): onPause
12-01 11:20:41.829: VERBOSE/Activity Lifecycle Main(353): onStop
SecondActivity Log:
12-01 11:20:41.438: VERBOSE/Activity Lifecycle Second(353): onCreate
12-01 11:20:41.459: VERBOSE/Activity Lifecycle Second(353): onStart
12-01 11:20:41.459: VERBOSE/Activity Lifecycle Second(353): onResume
Close the Second Activity
ExampleActivity Log:
12-01 11:25:48.658: VERBOSE/Activity Lifecycle Main(353): onRestart
12-01 11:25:48.658: VERBOSE/Activity Lifecycle Main(353): onStart
12-01 11:25:48.658: VERBOSE/Activity Lifecycle Main(353): onResume
SecondActivity Log:
12-01 11:25:48.619: VERBOSE/Activity Lifecycle Second(353): onPause
12-01 11:25:48.958: VERBOSE/Activity Lifecycle Second(353): onStop
12-01 11:25:48.968: VERBOSE/Activity Lifecycle Second(353): onDestroy
小結:1. onCreate之後: 可以做onRestoreInstanceState,來恢復UI之前保存的數據.
2. onPause之前: 可以做onSaveInstanceState,來保存UI當前的數據,以後將來恢復.
3. 在某些極端情況下, 可能會不出現onDestory, onStop, onPause而程序就被killed掉了,這也就是爲什麼onSaveInstanceState在onPause之前調用.
4. onPuase之後, 要過一些時間纔會調用onStop. 所以onPause之後,可以馬上調用onResume回來.
參考: Android 高級編程
http://androidappdocs.appspot.com/guide/topics/fundamentals.html#acttask
Have a good day ;v)