Activity Lifecycle

A list,

When a user browses, exits, and returns to your application, the Activity instance in your application transitions between different states in its lifecycle. The Activity class provides a number of callbacks that let the Activity know that a state has changed: the system is creating, stopping, or resuming an Activity, or destroying the process in which the Activity is running.

Learn about the Activity lifecycle

As the four components with the strongest user perception, Activity bears important tasks in App. Being familiar with the life cycle of Activity can help us understand the state of Activity in different situations. The concept of life cycle is ubiquitous in Android. Applications, Services, and Fragments all have their own life cycles. This article focuses on the basics of the Activity life cycle and does not cover too complicated parameters and behaviors.

Core processes and callback functions

Figure 2.1 Flow chart of Android life cycle

2.1 the onCreate ()

The onCreate() method is the first callback in the Activity lifecycle. Its normal operation signals that the Activity is being created, and we can perform some initialization operations in it, such as initializing the controller and setting related listening events, initializing the ViewModel, and so on. Paired with it is the onDestory() method.

2.2 onStart ()

The onStart() method indicates that the Activity is already in the foreground and visible. Many blogs/books refer to onStart() as visible. I think this is ambiguous, because visible does not mean visible, and the user cannot see the Activity when it runs onStart(). So why is onStart() visible? I think what it really means is that all the initialation-related code in the Activity has been executed, but it is not yet displayed in the foreground and cannot interact with the user. Its visibility is only logical, paired with the onStop() method. The onStart() method is a very short one, and it is best not to do too much in this lifecycle callback, as explained in the onResume() lifecycle analysis.

2.3 onResume ()

The execution of the onResume() method indicates that the user is ready to interact with the Activity. At this point, the View on the Activity’s layout has been drawn and the user can see the interface, which represents visual visibility. Paired with it is the onPause() method. After reading the meaning of onResume(), you should have your own answer as to why you can’t do much in onStart(). In order to ensure the user experience, the Android system must ensure that the interface is presented in front of the user in time. Just imagine that if onStart() does a lot of operations, the interface cannot be displayed to the user in a long time, then the APP user experience must be bad. Therefore, Google recommends not doing too much in onStart() to affect the display of the screen.

2.4 onPause ()

OnPause () indicates that the Activity has been paused but is still visible to the user. Typically, an Activity with a transparent theme (such as transparent ads) is displayed above an Activity with an opaque theme. The underlying activities that are overridden then enter onPause() and are no longer able to interact with the user, but are still visible to the user. Do not perform time-consuming operations in onPause() because it will affect the display of the next screen. For the same reason as onStart(). An Activity in the onPause() state can enter the onResume() state directly after it enters the foreground.

2.5 onStop ()

OnStop () indicates that the Activity is no longer visible to the user. For example, when Activity A starts an opaque Activity B, it enters the onStop() state. The difference with onPause() is that an Activity in the onPause() state is still visible to the user, while an Activity in the onStop() state is not. OnStop () allows for some heavy-duty recycling, but again, it shouldn’t be too time-consuming. If an application in the onStop() state is brought back to the foreground, it needs to go through the onRestart() state to return to the onStart() state to prepare and display to the foreground.

2.6 onDestory ()

OnDestory (), which indicates that the Activity is about to be destroyed, is the last callback in the Activity’s life cycle and usually does some simple recycling.

2.7 onRestart ()

This state occurs only when the Activity becomes visible again from being invisible, such as when the user clicks the Home button and switches back again.

Third, summary

The Activity lifecycle is an important concept to help developers understand the runtime state of an Activity, which comes in pairs and can be remembered in pairs.