Activities是由Activity stack管理的。当一个新的Activity被启动,它就会处于stack的top位置,成为当前运行的Activity。而前一个Activity依然保留在stack里面,当需要调用这个Activity时就会回到stack的top位置成为当前运行的Activity。
当一个activity从这个状态转变到另一个状态时,它被以下列protected方法所通知:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class Activity extends ApplicationContext {
protected void onCreate(Bundle savedInstanceState);
protected void onStart();
protected void onRestart();
protected void onResume();
protected void onPause();
protected void onStop();
protected void onDestroy();
}
|
1
2
3
|
05-08 09:39:48.389: DEBUG/Activity1(313): onCreate Activity 1
05-08 09:39:48.399: DEBUG/Activity1(313): onStart Activity 1
05-08 09:39:48.399: DEBUG/Activity1(313): onResume Activity 1
|
这说明一般Activity的启动顺序是onCreate -> onStart -> onResume
2. 点击back返回键后
1
2
3
|
05-08 09:40:04.129: DEBUG/Activity1(313): onPause Activity 1
05-08 09:40:04.628: DEBUG/Activity1(313): onStop Activity 1
05-08 09:40:04.659: DEBUG/Activity1(313): onDestory Activity 1
|
退出当前Activity时,onPause -> onStop -> onDestory
3. 再次启动Activity
1
2
3
|
05-08 09:40:18.249: DEBUG/Activity1(313): onCreate Activity 1
05-08 09:40:18.249: DEBUG/Activity1(313): onStart Activity 1
05-08 09:40:18.259: DEBUG/Activity1(313): onResume Activity 1
|
1
2
3
4
5
|
05-08 09:40:25.477: DEBUG/Activity1(313): onPause Activity 1
05-08 09:40:25.687: DEBUG/Activity2(313): onCreate Activity 2
05-08 09:40:25.687: DEBUG/Activity2(313): onStart Activity 2
05-08 09:40:25.719: DEBUG/Activity2(313): onResume Activity 2
05-08 09:40:26.277: DEBUG/Activity1(313): onStop Activity 1
|
1
2
|
05-08 09:40:31.777: DEBUG/Activity2(313): onPause Activity 2
05-08 09:40:32.658: DEBUG/Activity2(313): onStop Activity 2
|
Activity2.onPause - > Activity2.onStop
6. 回到原来的程序
1
2
3
4
|
05-08 09:40:50.429: INFO/ActivityManager(58): Starting activity: Intent ...
05-08 09:40:50.649: DEBUG/Activity2(313): onRestart Activity 2
05-08 09:40:50.649: DEBUG/Activity2(313): onStart Activity 2
05-08 09:40:50.769: DEBUG/Activity2(313): onResume Activity 2
|
1
2
3
4
5
6
|
05-08 09:41:04.928: DEBUG/Activity2(313): onPause Activity 2
05-08 09:41:04.988: DEBUG/Activity1(313): onRestart Activity 1
05-08 09:41:04.998: DEBUG/Activity1(313): onStart Activity 1
05-08 09:41:04.998: DEBUG/Activity1(313): onResume Activity 1
05-08 09:41:05.419: DEBUG/Activity2(313): onStop Activity 2
05-08 09:41:05.469: DEBUG/Activity2(313): onDestory Activity 2
|
1
2
3
|
05-08 09:41:51.868: DEBUG/Activity1(313): onPause Activity 1
05-08 09:41:52.428: DEBUG/Activity1(313): onStop Activity 1
05-08 09:41:52.468: DEBUG/Activity1(313): onDestory Activity 1
|
保存activity状态