Android Lifecycle Book, Android Lifecycle

The Android life cycle is divided into two parts: (The focus of the life cycle in abnormal situations is slightly different from that in typical situations)

1. The life cycle in a typical situation refers to the changes in the life cycle experienced by the Activity in the case of user participation. Under normal circumstances, Activity will go through the following life cycle:

1) onCreate(): We can do some initialization work, such as calling setContentView to load interface layout resources, initialize the data required by Activity, etc.

2) onRestart: Indicates that the Activity is being restarted. In general, onRestart is called when the current Activity changes from invisible to visible again. This situation is generally caused by user behavior. For example, the user presses the Home button to switch to the desktop or the user opens a new Activity. At this time, the current Activity will be suspended, that is, onPause and onStop are executed, and then the user returns to To this Activity, this will happen.

3) onStart: Indicates that the Activity is being started and is about to start. At this time, the Activity is already visible, but it has not yet appeared in the foreground and cannot interact with the user.

4) onResume: Indicates that the Activity is already visible, and appears in the foreground and starts the activity. Both onStart and onResume indicate that the Activity is already visible, but the Activity is still in the background when onStart is on, and the Activity is displayed in the foreground when onResume is on.

5) onPause: Indicates that the Activity is being stopped. Under normal circumstances, onStop will be called immediately. In special cases, if you quickly return to the current Activity at this time, onResume will be called. At this time, you can do some work such as storing data, stopping animation, etc., but be careful not to take too long, because this will affect the display of the new Activity, and the onResume of the new Activity will only be executed after onPause is executed. (The new Activity can be started after the new Activity needs to be onPause first, so the old Activity is onPause first, and then the new Activity is started)

6) onStop: Indicates that the Activity is about to stop, you can do some heavyweight recycling work, and it can't be too time-consuming.

7) onDestroy: Indicates that the Activity is about to be destroyed, which is the last callback in the Activity life cycle, and can do some recycling work and final resource release.

df917ffda830?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

In view of the above figure, here is a specific description, which is divided into the following situations:

1) For a specific Activity, the first time it starts, the callback is as follows: onCreate->onStart->onResume.

2) When the user opens a new Activity or switches to the desktop, the callback is as follows: onPause->onStop. There is a special case here. If the new Activity adopts a transparent theme, the current Activity will not call back onStop.

3) When the user opens a new Activity or switches to the desktop, the callback is as follows: onRestart->onStart->onResume.

4) When the user presses the back button to go back, the callback process is as follows: onPause->onStop->onDestroy.

5) When the Activity is re-opened after being recycled by the system, the callback process of the life cycle method is the same as 1). Note that only the life cycle method is the same, which does not mean that all processes are the same. It will be explained in detail in the life cycle of abnormal conditions.

6) From the whole life cycle, onCreate and onDestroy are paired, respectively marking the creation and destruction of Activity, and only one call is possible. From the perspective of whether the Activity is visible, onStart and onStop are paired. As the user operates or the device screen is turned on and off, these two methods may be called multiple times; from the perspective of whether the Activity is in the foreground, onResume and onPause are For pairing, these two methods may be called multiple times as the user operates or the device screen turns on and off.

Attachment: When a new Activity is started, the onPause of the old Activity will be executed first, and then the new Activity will be started. verify

df917ffda830?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

old activity

df917ffda830?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

new activity

df917ffda830?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

operation result

结论:不能在onPause中做重量级的操作,因为必须onPause执行完成后以后新的Activity才能Resume。onPause和onStop都不能执行耗时操作,尤其是onPause,这也意味着,我们应当尽量在onStop中操作,从而使得新的Activity尽快显示出来并切换到前台。

二、异常情况的生命周期,是指Activity被系统的回收或者由于当前设备的Configuration发生改变从而导致Activity被销毁重建。

1.情况1:资源相关的系统配置发生改变时,导致Activity被杀死并重新创建。

拿最简单的图片来说,当我们把一张图片放在mipmap目录后,就可以通过Resource去获取这张图片,同时为了兼容不同的设备,我们可能还需要在其他的一些目录放置不同的图片,比如mipmap-mdpi、mipmap-hdpi等,这样当程序启动时,系统就会根据当前设备的情况去加载合适的Resource资源,比如说横屏手机和竖屏手机会拿到两张不同的图片(设定了landscape或者portrait状态下的图片)。比如说当前的Activity处于竖屏状态,如果突然旋转屏幕,由于系统配置发生了改变,默认情况下、Activity就会被销毁并且重新创建,当然我们可以阻止系统重新创建我们的Activity。

df917ffda830?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

异常情况下的Activity的重建过程

系统会调用onSaveInstanceState来保存当前Activity的状态,这个方法的调用时机在onStop之前,它和onPause没有既定的时序关系,既可能在onPause之前调用,也可能在onPause之后调用。正常情况系统不会回调这个方法!当Activity被重新创建后,系统就会调用onRestoreInstanceState,并且把Activity销毁时onSaveInstanceState方法所保存的Bundle对象作为参数同时传递给onRestoreInstanceState和onCreate方法,从时序上来看,onRestoreInstanceState的调用时机在onStart之后。

在onSaveInstance和onRestoreInstanceState方法中,系统自动为我们做了一定的恢复工作。当Activity在异常情况下需要重新创建时,系统会默认为我们保存当前Activity的视图结构,并且在Activity重启后为我们恢复这些数据,比如文本框中用户输入的数据、ListView滚动的位置等,这些View相关的状态,系统都能够默认为我们恢复。系统能够自动地做一些View层次结构方面的数据存储和恢复。比如TextView可以保存文本选中的状态和文本内容。

eg:

df917ffda830?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

验证数据存储和恢复的情况

df917ffda830?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

系统日志

首先我们在onSaveInstance中存储一个字符串,然后当Activity被销毁并重新创建后,我们再去获取之前存储的的字符串。接手的位置可以是onRestoreInstanceState或者onCreate。二者的区别是:onRestoreInstanceState一旦被调用,其参数Bundle savedInstanceState一定是有值的,我们不用额外地判断是否为空;但是onCreate不行,onCreate如果是正常启动的话,其参数Bundle savedInstanceState为null,所以必须要额外的判断。官方文档建议采用onRestoreInstanceState去恢复数据。

注意:系统只在Activity异常终止的时候才会调用onSaveInstanceState和onRestoreInstanceState来存储和恢复数据,其他情况不会触发这个过程。

2.情况2:资源内存不足导致低优先级的Activity被杀死(数据存储和恢复过程和情况一完全一致)

Activity按优先级从高到低,可以分为如下三种情况:

1)前台Activity:正在和用户交互的Activity,优先级最高。

2)可见但非前台Activity-比如Activity中弹出了一个对话框,导致Activity可见但是位于后台无法和用户直接交互。

3)后台Activity-已经被暂停的Activity,比如执行了onStop。优先级最低。

问题:当系统配置发生改变,如何阻止系统重新创建Activity?指定ConfigChanges属性

df917ffda830?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

在清单文件中的配置

由于编译时指定的minSDKVersion和targetSDKVersion有一个大于13,所以为了防止旋转屏幕时Activity重启,除了orientation,我们还要加上screenSize。

df917ffda830?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

配置后调用的方法和日志

Conclusion: The Activity is indeed not recreated, and onSaveInstanceState and onRestoreInstanceState are not called to store and restore data. Instead, the system calls the onConfigurationChanged method. At this time, we can do some of our own processing.

Attached:

Three common items and meanings of configChanges:

1) local The local location of the device has changed, generally referring to switching the system language.

2) keyboardHidden The accessibility of the keyboard has changed, such as when the user brings up the keyboard.

3) orientation The screen orientation has changed, such as rotating the phone screen. Reference: Exploring the Art of Android Development

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324078367&siteId=291194637