Activity的生命周期分为两部分:1,典型情况下的生命周期,指在用户参与的情况下Activity所经过的生命周期的改变;2,异常情况下的生命周期,指Activity被系统回收或者当前设备的Configuration发横改变从而导致Activity被销毁重建。
Life cycle in typical situations
在正常情况下,生命周期如下:
OnCreate
表示Activity正在被创建,这是生命周期的第一个方法。在这个方法里可以做一些初始化工作,比如调用setContentView去加载界面布局,初始化Activity所需的数据等。
Onrestart
表示Activity正在重新启动。一般情况下,当当前Activity从不可见到重新变为可见状态时,onRestart方法会被调用。这种情况一般是用户导致的,比如用户按Home键切换到桌面或用户打开一个新的Activity,当前的Activity就会暂停,也就是onPause和onStop被执行了,接着用户又回到了这个Activity,就会出现这种情况了。
OnStart
表示Activity正在被启动,即将开始,这时Activity已经可见了,但是还没有出现在前台,还无法和用户交互。也就是Activity已经出现了,但我们还不能看到。
Onresume
表示Activity已经可见了,并且出现在前台开始活动了。
OnPause
表示Activity正在停止,正常情况下,接着onStop就会被调用。此时可以执行一些存储数据,停止动画的操作,但是注意不能太耗时,因为这个方法结束,下一个Activity的onResume才能执行,如果时间太长会造成Activity切换卡顿。
OnStop
表示Activity即将停止,可以做一些重量级的回收,但最好也不要太耗时。
OnDestroy
表示Activity即将被销毁,这是最后一个回调,做所有资源的释放和回收。
Classic Pictures:
Specific explanations
1, for a specific activity, the first start, callback is Oncreate->onstart->onresume.
2, when the user opens a new activity or switches to the desktop, the callback is onpause->onstop. There is a special case where the current activity does not callback OnStop if the new activity uses a transparent theme.
3, when the user returns to activity again, the return is Onrestart->onstart->onresume.
4, when the user presses the back key, the callback is Onpause->onstop->ondestroy.
5, when the activity is re-opened after the system is reclaimed, the life cycle callback process and (1) are the same. is the life cycle approach, which does not represent the same process.
Life cycle in exceptional situations
我们应该能够猜到,Activity除了被用户杀死外还有可能被系统杀死。情况如下:
Scenario 1: Resource-related configuration changes cause activity to be killed and recreated
拿最简单的图片资源来说吧,手机横屏和竖屏时加载的图片可能不是一个资源,那么此时就会导致系统配置发生改变,在默认情况下,Activity会被销毁并且重新创建,当然我们可以组织系统重新创建我们的Activity。Activity出现意外情况->onSaveInstanceState然后又两种情况 ->onDestroy ->onCreate->onRestoreInstanceState当前Android已经做了一定程度的数据恢复了。当然也可以重写onSaveInstanceState和onRestoreInstanceState方法来实现自定义的数据的恢复。
Scenario 2: Insufficient resource memory causes low priority activity to be recycled
这种情况不容易模拟,但是很容易能够想到。1,前台Activity——正在和用户交互,优先级最高。2,可见但非前台Activity——比如弹出了一个对话框,导致Activity无法和用户直接交互。3,后台Activity——已经被暂停的Activity,优先级最低。这些也会回调onSaveInstanceState和onRestoreInstanceState来存储和恢复数据。一些工作不适合放在后台Activity中执行,最好是放进Service中执行。
Prevent re-creation of activity due to system configuration changes
Configure in Mainfest:
android:configChanges="orientation"
If you want to specify multiple values, use the ' | ' To connect.
Like what
android:configChanges="orientation|keyboardHidden"
This configuration item is very numerous and is described in more detail below:
| Project |
meaning |
The
| MCC |
SIM card uniquely identifies the country code in the IMSI (International Mobile User ID), which consists of three digits, and China is 460. This identifies the MCC changes |
| MNC |
SIM card uniquely identifies the carrier code in IMSI (International Mobile User ID), consisting of two digits, China Mobile TD is 00, Chinese unicom is 01, Telecom is 03. This entry identifies an MNC change |
| locale |
device's local location has changed, generally refers to switching the system language |
| touchscreen |
Ignore it |
| keyboard |
keyboard type has changed, such as using the external keyboard |
The accessibility of the
| keyboardhidden |
Keyboard has changed if the external keyboard is unplugged |
| navigation |
ignore it |
| screenlayout |
screen layout has changed, such as activating another screen |
| fontscale |
system font changed |
The
| uimode |
user interface has changed, such as to turn on night mode (API8 add) |
| Orientation |
screen orientation has changed |
| screensize |
rotation can also cause this change |
| smallestscreensize |
ignore him |
| layoutdirection |
ignore him |
Android-activity life cycle Comprehensive analysis