Android activity survival, Android activity survival
The Activity class defines seven callback methods, covering every step of the Activity lifecycle. Let's take a look at them one by one.
This section describes the seven methods.
1. onCreate ()
You have seen this method many times, and we have rewritten this method in every activity, it will be in the activity
Called when it is created for the first time. You should initialize the activity in this method, such as loading the cloth.
Bureau, binding events, etc.
2. onStart ()
This method is called when the activity changes from invisible to visible.
3. onResume ()
This method is called when the activity is ready to interact with the user. At this time, the activity must be in
Stack top and running.
4. onPause ()
This method is called when the system is ready to start or resume another activity. We usually
Method To release CPU-consuming resources and save some key data, but the execution speed of this method is
It must be fast, otherwise it will affect the use of new stack top activities.
5. onStop ()
This method is called when the activity is completely invisible. The main difference between it and the onPause () method is that, for example
If the new activity is a dialog box activity, the onPause () method will be executed, while onStop ()
Method is not executed.
6. onDestroy ()
This method is called before the activity is destroyed, and the activity status changes to the destruction status.
7. onRestart ()
This method is called before the activity changes from stopped to running, that is, the activity is restarted.
In addition to the onRestart () method, the other seven methods are opposite to each other, so that the activity can be divided into three
.
1. Complete survival
The lifetime of an activity between the onCreate () method and the onDestroy () method is complete. General
In the onCreate () method
To release the memory.
2. Visible lifetime
The activity experienced between the onStart () method and the onStop () method is the visible lifetime. Survive in the visible
Activities are always visible to users during the period, even if they may not interact with users. We can use these two
Methods to manage resources that are visible to users. For example, loading resources in the onStart () method,
In the onStop () method, resources are released to ensure that the stopped activity does not occupy too much memory.
3. Foreground survival
What an activity experiences between the onResume () method and the onPause () method is the foreground survival. At the front-end
During the lifetime, activities are always running. At this time, activities can interact with users.
The most active activities are also in this status.