Android Activity life cycle

Source: Internet
Author: User

Android Activity life cycle

1. Basic knowledge

The 7 life cycle methods need to be called at different stages during activity from establishment to destruction. The 7 life cycle methods are defined as follows:
protected void OnCreate (Bundle savedinstancestate)
protected void OnStart ()
protected void Onresume ()
protected void OnPause ()
protected void OnStop ()
protected void Onrestart ()
protected void OnDestroy ()

The above 7 life cycle methods are called in a certain order in 4 stages, and these 4 phases are as follows:
Start activity: Perform 3 life cycle methods in sequence at this stage: OnCreate, OnStart, and Onresume.
Activity loses focus: If you enter another activity or application while the activity is focused, the current activity loses focus. At this stage, the OnPause and OnStop methods are executed in turn.
Activity regain focus: If activity regain focus, 3 life cycle methods are executed sequentially: Onrestart, OnStart, and Onresume.
Close activity: When activity is closed, the system executes 3 life cycle methods: OnPause, OnStop, and OnDestroy.
If there is no state change during these 4 phases of the life cycle approach, then the system will execute the life cycle methods in the 4 stages as described above, but if the state is changed during execution, the system will invoke the life cycle method in a more complex way.

The life cycle methods that can change the execution trajectory of the system during execution are OnPause and OnStop. If the activity gets the focus again during the execution of the OnPause method, it loses focus. Instead of executing the OnStop method, the system executes the corresponding life cycle method in the following order:
OnPause, onresume-> OnPause
If the activity gets the focus again during the execution of the OnStop method, it loses focus. Instead of executing the OnDestroy method, the system executes the corresponding life cycle method in the following order:
OnPause, Onresume, OnStart, Onrestart, OnStop, OnStop
Describes this process in detail.

It is not difficult to see from the activity life cycle shown that this diagram contains a two-layer loop,
The first loop is OnPause, Onresume, OnPause,
The second loop is OnPause, Onresume, OnStop, OnStart, Onrestart, OnStop, and so on. We can consider these two layers of loops as the sub-lifecycles of the integration activity lifecycle. The first loop is called the focus life cycle, and the second layer is called the visual life cycle. In other words, the first loop loops through the acquisition and loss of activity focus, during which the activity is always visible. The second layer loops in the process of visible and invisible activity, which is accompanied by the acquisition and loss of the focus of activity. In other words, the activity is first displayed, then the focus is lost, then the focus is removed, and finally the activity becomes invisible due to the other activity being ejected. Therefore, activity has the following 3 life cycles:
Overall life cycle: onCreate---OnDestroy.
Visual life cycle: OnStop---OnPause.
Focus life cycle: OnPause-Onresume.
Note: As can be seen in the activity lifecycle shown in Figure 1, the system invokes the OnPause, OnStop, and Ondesktroy methods when terminating an application process. The OnPause method is at the top of the line, meaning that the activity may be terminated when the focus is lost, while the OnStop and OnDestroy methods may not have a chance to execute. Therefore, the current activity state should be saved in the OnPause method to ensure that the code that holds the activity state can be executed at any time when the process is terminated.
Description of the activity's main life cycle method:
OnCreate (Bundle savedinstancestate): Called when an activity is created. Set in this method, it also provides access to any previously stored state in the form of bundles!
OnStart (): Called when activity becomes visible to the user on the screen.
Onresume (): Called when activity starts interacting with the user (whether it is starting or restarting an activity, the method is always called).
OnPause (): Activity is paused or recalled CPU and other resources when called, the method is used to save the active state, but also to protect the site, press stack bar!
OnStop (): Called when activity is stopped and turned into an invisible phase and subsequent life-cycle events.
Onrestart (): Called when activity is restarted. The activity is still in the stack, not the start of a new activity.
OnDestroy (): Called when activity is completely removed from system memory, the method is called probably because someone calls the OnFinish () method directly or the system decides to stop the activity to free up resources!

2. Event method Chain
2.1 Entering activity
OnCreate, OnStart, Onresume
2.2 Back Key
OnPause, OnStop, OnDestroy
2.3 Home Key
Home key exit: OnPause, OnStop
Home button back: Onrestart, OnStart, Onresume
2.4 Hibernation/recovery
Hibernation: OnPause
Recovery: Onresume
2.5 Rotating the screen
Android:configchanges Not set:
OnStart, OnCreate, Ondestory, OnStop, OnPause, Onresume
Android:configchanges= "Orientation|keyboardhidden" is set:
The life cycle method is not triggered, see article here.
2.6来 Electric
Caller ID, show caller interface:
OnPause-OnStop
Close the phone interface and return to the current activity:
Onrestart, OnStart, Onresume
2.7 Other activity
Go to the next activity:
OnPause-OnStop
Return from other activity to current Acitivity:onrestart, OnStart, Onresume

3. Application scenarios combined with Activity Lifecycle
3.1 with broadcast (broadcast)
on The resume registers the broadcast (Registerlinstener), and the OnPause unregisters the broadcast (Unregisterlinstener). For example:
Do "shake" function (sensor), monitoring network changes, you can register in the Onresume monitoring, write-off in the OnPause, has saved resources to improve efficiency.
3.2 Binds the Service (service)
in the OnStart binding services (Bindservice), unbinding (Unbindservice) in OnStop. For example:
The data on the UI needs to be updated through the service timing, and the activity's visible period between OnStart and OnStop can start the service again OnStart and stop the service at OnStop. In addition to improving the user experience, developers should optimize the program as much as possible in order to conserve system resources.
3.3 with cursor
uses managedquery to let activity help you manage the cursor life cycle without having to close it yourself. But there are some questions that add two articles: here 1, here 2.
3.4 Frees resources
to release some resources in Ondestory. For example, you can call MediaPlayer's release at Ondestory.
4, notice that
4.1 All activity lifecycle methods must first call their parent version.
4.2 Because activity often toggles between pausing and resuming, the two methods of Onresume and OnPause should be lightweight.
4.3 In some emergency situations in the system need to reclaim memory, OnStop, ondestory may not be called, so you need to save long-term data in OnPause.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.