Hello, everyone. Last time we talked about the Activity example in Android. This time we will continue with that example. Stop gossiping and return to the truth. Let’s Talk Android!
Hello, long time no see. After the Acitivity example last time, some viewers wanted to learn more about the Activity life cycle, so let’s pick up where we left off. This is getting a little long, so if you forgot, you can bring up the Acitivity life cycle graph from last time. We’ll talk about it in terms of the graphics.
Before we get into the Activity lifecycle, let’s take a look at the state of the Activity. It has the following six operating states:
-
Initialization (init)Copy the code
-
Start (start)Copy the code
-
Run (running)Copy the code
-
Stop (pause)Copy the code
-
Stop (stop)Copy the code
-
Destruction (destroy)Copy the code
Next, we introduce each of the six states.
Initialization (init)
This state is used to initialize activity-related tasks, such as loading controls in the layout. This state corresponds to the onCreate method in the lifecycle. When the Activity is in this state, the onCreate method is called back, so we can do some work related to the Activity initialization in this method.
Start (start)
In this state, the Activity is loaded until it can be seen. This state corresponds to the onStart method in the lifecycle. When the Activity is in this state, the onStart method is called back.
Run (running)
In this state, the Activity gains focus and we can operate on the Activity. The Activity also responds to our actions. This state corresponds to the onResume method in the lifecycle. When the Activity is in this state, the onResumes method is called. So we can handle things in this method that respond to Activity operations, such as setting up event listeners.
Stop (pause)
In this state, we can still see the Activity, but we can’t get focus from the Activity, and we need to save the data from the Activity in this state for use when the Activity runs again. This state corresponds to the onPause method in the lifecycle. When the Activity is in this state, the onPause method is called back. Therefore, we can save data related to the Activity in this method.
Stop (stop)
In this state, the Activity is removed until we can no longer see it. This state corresponds to the onStop method in the lifecycle. When the Activity is in this state, the onStop method is called back.
Destruction (destroy)
Resources associated with the Activity are released in this state. This state corresponds to the onDestroy method in the lifecycle. When the Activity is in this state, the onDestroy method is called back. So we can deal with things related to releasing resources in this method, such as unregistering broadcast receivers, destroying services, and so on.
I think you can use the process state to understand the state of an Activity, because an Activity is also a process from the perspective of a process. In addition, the states I introduce here are different from the official states, which are only three: Run, pause, and Stop. By contrast, the run, pause, and stop in the official state is the same as the run, pause, and stop in our introduction. We have introduced three more states than the official ones, which we have added to make it easier for you to understand. After adding these states, each state will correspond to a callback method, which will help you understand how to use different callback methods. In terms of the accuracy of knowledge, we should take the official content as the criterion.
Last but not least, I have some personal opinions: I feel that the state given by the official is incomplete. It is run directly without initialization, which is obviously unreasonable. Of course, the authorities may have other reasons. This is also my opinion, welcome everyone to discuss.
This is an example of an Activity in Android. If you want to know more about it, listen to the next time.