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:
onCreate()
through to a single final call to onDestroy()
. An activity does all its initial setup of "global" state in onCreate()
, and releases all remaining resources in onDestroy()
. For example, if it has a thread running in the background to download data from the network, it may create that thread in onCreate()
and then stop the thread in onDestroy()
.The visible lifetime of an activity happens between a call to
until a corresponding call to onStart()
. 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 a onStop()
BroadcastReceiver
in onStart()
to monitor for changes that impact your UI, and unregister it in onStop()
when the user can no longer see what you are displaying. The onStart()
and onStop()
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 to onResume()
. 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
public class ExampleActivity extends Activity { private static final String TAG = "Activity Lifecycle Main"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v(TAG, "onCreate"); setContentView(R.layout.main); Button btNext = (Button) findViewById(R.id.next_activity); btNext.setOnClickListener(new Button.OnClickListener() { public void onClick(View view) { startActivity(new Intent(ExampleActivity.this, SecondActivity.class)); } }); } @Override protected void onStart() { super.onStart(); Log.v(TAG, "onStart"); } @Override protected void onRestart() { super.onRestart(); Log.v(TAG, "onRestart"); } @Override protected void onResume() { super.onResume(); Log.v(TAG, "onResume"); } @Override protected void onPause() { super.onPause(); Log.v(TAG, "onPause"); } @Override protected void onStop() { super.onStop(); Log.v(TAG, "onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.v(TAG, "onDestroy"); } }
The Second Activity UI code of
SecondActivity.java
public class SecondActivity extends Activity { private static final String TAG = "Activity Lifecycle Second"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v(TAG, "onCreate"); setContentView(R.layout.second); Button btMain = (Button) findViewById(R.id.previous_activity); btMain.setOnClickListener(new Button.OnClickListener() { public void onClick(View view) { finish(); } }); } @Override protected void onStart() { super.onStart(); Log.v(TAG, "onStart"); } @Override protected void onRestart() { super.onRestart(); Log.v(TAG, "onRestart"); } @Override protected void onResume() { super.onResume(); Log.v(TAG, "onResume"); } @Override protected void onPause() { super.onPause(); Log.v(TAG, "onPause"); } @Override protected void onStop() { super.onStop(); Log.v(TAG, "onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.v(TAG, "onDestroy"); } }
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)