Android-Activity life cycle

1. Under normal circumstances, an Activity will go through the following 7 life cycles
1: onCreate: This method is called when the Activity is run for the first time, which can be used to load the layout view, get the control namespace and other initialization work.
2: onRestart: When the Activity is restarted, call this method
3: onStart: Indicates that the Activity is being started and has never been visible (not visible to the user, it means that the Activity is running in the background and does not appear in the foreground), But still unable to interact with the user.
4: onResume: The table Activity has become visible, and it appears to work in the foreground, which means that the user is visible.
5: onPause: It means that the Activity is paused, but the Activity is still visible. Some lightweight operations can be performed, but generally Do not do too much, because this will affect the user experience.
6: onStop: Indicates that the Activity is about to be suspended. At this time, the Activity is working in the background and is no longer visible. You can do some lightweight operations like the onPause method, but still not too time-consuming.
7: onDestroy: Indicates that the event is about to be destroyed.

This picture fully reflects the entire life cycle of an Activity.

OnStart and onResume, the gap between onPause and onStop. The
former is about whether the Activity is visible, and the latter is about whether the Activity is in the foreground
. There is not much difference in actual use. For details, please refer to the official manual.
2. The life cycle of the Activity under
abnormal conditions is generally divided into two types
: 1: System configuration changes: In
this case, the most common is that the screen rotates, causing the Activity to be killed. Re-create, the following picture can fully reflect.
When this exception occurs, first call onPause, then call the onSaveInstanceState() method to save the data, then recreate the Activity, and then call onRestoreInstanceState() to restore the data of the previous Activity.

2: Insufficient resource memory: In this case, the data storage and recovery are the same as before. But the priority of killing Activity is divided into the following three types:
1: Activity is in the foreground-the user can see and can interact, the highest priority
2: Activity is not in the foreground-the Activity is visible but the user cannot interact ()
3: Activity In the background-invisible and unable to interact with the user, such as executing onStop (), the lowest priority.

Guess you like

Origin blog.csdn.net/News53231323/article/details/113917191