先来一张经典的生命周期图:
-------------------------------------------------------------
工程代码: ActivityLifeCycle.zip
--------------------------------------------------------------
1. 启动Activity 1:
08-24 17:24:51.760: D/CARLOZ-A1(20109): onCreate 08-24 17:24:51.760: D/CARLOZ-A1(20109): onStart 08-24 17:24:51.760: D/CARLOZ-A1(20109): onResume
1.1. 返回键 关闭Activity 1:
08-24 17:25:45.960: D/CARLOZ-A1(20109): onPause 08-24 17:25:46.790: D/CARLOZ-A1(20109): onStop 08-24 17:25:46.790: D/CARLOZ-A1(20109): onDestroy
1.2 HOME键回主屏,再从Recent Task中重新打开Activity1
08-24 17:26:37.610: D/CARLOZ-A1(20109): onPause 08-24 17:26:38.340: D/CARLOZ-A1(20109): onStop 从recent task中重新打开Activity1 08-24 17:26:59.380: D/CARLOZ-A1(20109): onRestart 08-24 17:26:59.380: D/CARLOZ-A1(20109): onStart 08-24 17:26:59.380: D/CARLOZ-A1(20109): onResume
1.3 HOME键回主屏,从Recent Task中杀死Activity1, 在从Launcher打开Activity1
08-24 17:28:06.670: D/CARLOZ-A1(20109): onPause 08-24 17:28:07.450: D/CARLOZ-A1(20109): onStop 08-24 17:28:15.860: D/CARLOZ-A1(22178): onCreate 08-24 17:28:15.860: D/CARLOZ-A1(22178): onStart 08-24 17:28:15.860: D/CARLOZ-A1(22178): onResume
2. 两个Activity切换
2.1 Activity1 打开Activity 2,再用返回键返回Activity1
08-24 17:32:33.150: D/CARLOZ-A1(24696): onPause 08-24 17:32:33.200: D/CARLOZ-A2(24696): onCreate 08-24 17:32:33.210: D/CARLOZ-A2(24696): onStart 08-24 17:32:33.210: D/CARLOZ-A2(24696): onResume 此时Activity2 完全覆盖Activity 1 08-24 17:32:33.750: D/CARLOZ-A1(24696): onStop 从Activity 2 返回Activity 1 08-24 17:34:06.330: D/CARLOZ-A2(24696): onPause 08-24 17:34:06.360: D/CARLOZ-A1(24696): onRestart 08-24 17:34:06.370: D/CARLOZ-A1(24696): onStart 08-24 17:34:06.370: D/CARLOZ-A1(24696): onResume 08-24 17:34:06.790: D/CARLOZ-A2(24696): onStop 08-24 17:34:06.790: D/CARLOZ-A2(24696): onDestroy
2.2 将Activity2 换成 Dialog(不能完全覆盖Activity1)
08-24 17:37:39.680: D/CARLOZ-A1(26589): onPause 08-24 17:37:39.740: D/CARLOZ-A2(26589): onCreate 08-24 17:37:39.740: D/CARLOZ-A2(26589): onStart 08-24 17:37:39.740: D/CARLOZ-A2(26589): onResume 可以看到,Activity1 不再执行onStop 从Activity2返回Activity1 08-24 17:38:15.180: D/CARLOZ-A2(26589): onPause 08-24 17:38:15.220: D/CARLOZ-A1(26589): onResume 可以看到Activity1 也不再执行onStart方法 08-24 17:38:15.290: D/CARLOZ-A2(26589): onStop 08-24 17:38:15.290: D/CARLOZ-A2(26589): onDestroy
2.2.1 Activity2是dialog, 点击Recent Task
08-24 17:40:42.000: D/CARLOZ-A2(26589): onPause 08-24 17:40:42.010: D/CARLOZ-A1(26589): onStop 08-24 17:40:42.080: D/CARLOZ-A2(26589): onStop 从Recent Task状态返回 08-24 17:41:24.430: D/CARLOZ-A1(26589): onRestart 08-24 17:41:24.440: D/CARLOZ-A1(26589): onStart 08-24 17:41:24.440: D/CARLOZ-A2(26589): onRestart 08-24 17:41:24.440: D/CARLOZ-A2(26589): onStart 08-24 17:41:24.440: D/CARLOZ-A2(26589): onResume
3. 单个Activity转屏
08-24 17:43:56.050: D/CARLOZ-A1(26589): onPause 08-24 17:43:56.050: D/CARLOZ-A1(26589): onStop 08-24 17:43:56.050: D/CARLOZ-A1(26589): onDestroy 08-24 17:43:56.210: D/CARLOZ-A1(26589): onCreate 08-24 17:43:56.210: D/CARLOZ-A1(26589): onStart 08-24 17:43:56.210: D/CARLOZ-A1(26589): onResume
4. Activity2是Dialog的状态下转屏
08-24 17:43:11.470: D/CARLOZ-A2(26589): onPause 08-24 17:43:11.470: D/CARLOZ-A2(26589): onStop 08-24 17:43:11.470: D/CARLOZ-A2(26589): onDestroy 08-24 17:43:11.630: D/CARLOZ-A2(26589): onCreate 08-24 17:43:11.630: D/CARLOZ-A2(26589): onStart 08-24 17:43:11.630: D/CARLOZ-A2(26589): onResume 08-24 17:43:11.650: D/CARLOZ-A1(26589): onStop 08-24 17:43:11.650: D/CARLOZ-A1(26589): onDestroy 08-24 17:43:11.780: D/CARLOZ-A1(26589): onCreate 08-24 17:43:11.790: D/CARLOZ-A1(26589): onStart 08-24 17:43:11.800: D/CARLOZ-A1(26589): onResume 08-24 17:43:11.820: D/CARLOZ-A1(26589): onPause
注意: 有Dialog主题的Activity2会导致 Activity1执行 onPause,但是纯粹的Dialog却不能触发Activity1执行onPause
-------------------------------------------------------------
工程代码: ActivityLifeCycle.zip
--------------------------------------------------------------