Activity生命周期总结

概述:

先祭出神图:


Activity生命周期总结_第1张图片

从上面的图中可以看到一些东西

  • onResume:Activity快要显示了,但是官方说用这种方式判断是否显示不准确,应该使用onWindowFocusChanged来监听。
  • onPause:是失去焦点不可与用户交互
  • onStop:是Activity不可见的分界线,比如被一个透明的activity覆盖就不会调用原activity的onStop方法

然后从几个场景分析

创建一个Activity,这个创建的Activity的生命周期:

07-19 17:01:04.555 11692-11692/com.ironman.activitylifecycle D/activity-life-cycle: onCreate
07-19 17:01:04.666 11692-11692/com.ironman.activitylifecycle D/activity-life-cycle: onStart
07-19 17:01:04.667 11692-11692/com.ironman.activitylifecycle D/activity-life-cycle: onResume

从一个activity打开另一个activity

07-19 17:38:45.576 12000-12000/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onPause
07-19 17:38:45.617 12000-12000/com.ironman.activitylifecycle D/activity-life-cycle: ScrollingActivity-onCreate
07-19 17:38:45.757 12000-12000/com.ironman.activitylifecycle D/activity-life-cycle: ScrollingActivity-onStart
07-19 17:38:45.758 12000-12000/com.ironman.activitylifecycle D/activity-life-cycle: ScrollingActivity-onResume
07-19 17:38:46.395 12000-12000/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onStop

会先运行当前activity的onPause,让他失去焦点不能与用户交互,然后调用创建以及显示第二个activity的方法,然后调用第一个activity的onStop方法让第一个Activity不显示。

官方文档里有一段对onPause方法的描述:

Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume().When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.
也就是不要在onPause里做太多耗时的工作,如果启动另一个activity,他会等着当前activity的onPause执行完才会去创建另一个Activity。

从一个activity打开一个透明的activity

07-19 18:55:09.797 10352-10352/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onPause
07-19 18:55:09.824 10352-10352/com.ironman.activitylifecycle D/activity-life-cycle: TransparentActivity-onCreate
07-19 18:55:09.829 10352-10352/com.ironman.activitylifecycle D/activity-life-cycle: TransparentActivity-onStart
07-19 18:55:09.830 10352-10352/com.ironman.activitylifecycle D/activity-life-cycle: TransparentActivity-onResume

可以看到前一个activity的onStop是没有被调用的。

从一个activity打开一个dialog

打开一个dialog并不会触发activity生命周期,dialog是这个activity内部的view并不会导致这个activity显示或者隐藏。

点击home键

07-19 18:24:53.644 18605-18605/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onPause
07-19 18:24:54.023 18605-18605/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onStop

从home键返回:

07-19 19:28:55.453 18586-18586/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onRestart
07-19 19:28:55.464 18586-18586/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onStart
07-19 19:28:55.464 18586-18586/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onResume

按返回键

1. 从第二个页面返回到第一个页面:

07-19 18:12:31.523 18605-18605/com.ironman.activitylifecycle D/activity-life-cycle: ScrollingActivity-onPause
07-19 18:12:31.535 18605-18605/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onRestart
07-19 18:12:31.535 18605-18605/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onStart
07-19 18:12:31.536 18605-18605/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onResume
07-19 18:12:32.022 18605-18605/com.ironman.activitylifecycle D/activity-life-cycle: ScrollingActivity-onStop
07-19 18:12:32.022 18605-18605/com.ironman.activitylifecycle D/activity-life-cycle: ScrollingActivity-onDestroy

可以看到先调用的是当前acitivity的onPause,然后在恢复前一个activity,前一个activity是从onRestart来的,所以只是stop了并没有销毁,当前一个activity的onResum运行完毕了之后返回的那个activity就被销毁了。

2.如果是从透明的activity返回:

07-19 19:00:02.664 10352-10352/com.ironman.activitylifecycle D/activity-life-cycle: TransparentActivity-onPause
07-19 19:00:02.687 10352-10352/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onResume
07-19 19:00:02.717 10352-10352/com.ironman.activitylifecycle D/activity-life-cycle: TransparentActivity-onStop
07-19 19:00:02.717 10352-10352/com.ironman.activitylifecycle D/activity-life-cycle: TransparentActivity-onDestroy

相对于非透明的activity,少运行了onRestart和onStart两个方法

按返回键退出app:

07-19 18:18:50.679 18605-18605/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onPause
07-19 18:18:51.067 18605-18605/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onStop
07-19 18:18:51.073 18605-18605/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onDestroy

其实就是单页面的销毁的生命周期,依次onPause, onStop, onDestroy

关闭屏幕(也就是点击电源键)

07-19 19:01:45.613 10352-10352/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onPause
07-19 19:01:45.676 10352-10352/com.ironman.activitylifecycle D/activity-life-cycle: MainActivity-onStop

总结一下

  • 创建一个activity会依次执行onCreate, onStart, onResume
  • 从一个activity启动另一个activity会先调用当前activity的onResume,然后在创建并显示要启动的activity。
  • 从非透明activity返回的时候会先调用当前的onPause,然后调用前一个activitiy的onRestart、onStart、onResme,然后在调用这个Activity的onStop,onDestroy。
  • 从透明的activity返回的时候会先调用当前activity的onPause,然后调用前一个activity的onResume,然后调用这个透明activity的onStop,onDestroy。
  • 透明的Activity比较特殊,他会让别的activity的一些生命周期不被调用比如onStop等待。
  • onResume方法里不能有太耗时的操作,因为当启动另一个activity的时候,当前的onResume没有返回不会去创建要启动的那个activity。

你可能感兴趣的:(Activity生命周期总结)