Activity lifecycle and status saving

Source: Internet
Author: User
The system dialog box is displayed, and some programs are still visible.
Onpause
When the dialog box disappears
Onresume
 
Call a new activity. When the old activity is invisible
Onpause-> onstop
When a new activity is returned
Onrestart-> onstart-> onresume
 
When a normally running activity ends normally
Onpause-> onstop-> ondestroy
 
Onresume
This is not the best way to determine whether the activity is visible to users. Even after this method is called, some system Windows may be in front of our activity, such as keyguard. Onwindowfocuschanged is the most accurate.
For some methods that are not passed to the System-managed cursor through a method like managedquery (android.net. Uri, string [], String, string [], string), we should re-obtain them at this time. Because, theoretically, you deactive them during onstop.
 
If activity a starts Activity B, the system will first call onpause of Activity A and then call oncreate, onstart, and onresume of Activity B. Then the onstop method of a is called.
Therefore, before the onpause of the old activity is returned, the new activity will not be created. Therefore, do not do things that are too time-consuming.
In the onpause method, we recommend that you stop the animation, which consumes a lot of CPU, so that the activity can be switched more quickly, and turn off the resources exclusively accessed by the camera.
 
When a background activity is terminated by the system due to resource shortage, no methods are called regardless of whether it is visible or not, except onsaveinstancestate.
 
When killprocess is killed by Android. OS. process. killprocess, no method is called, including onsaveinstancestate.

 

 

Save the status:

Onsaveinstancestate

When the activity is switched to the background (partially visible or invisible, but it has not interacted with the user), it may be killed due to resource shortage and this method will be called.

If this method is called, it will be before onstop, but it is not determined who is the first with onpause.

In the default implementation of this method, the status of the currently visible view tree is saved. If you want to retain this feature during rewriting, call Super. onsaveinstancestate.

 

Onrestoreinstancestate

This method is called between onstart and onresume.

In most cases, you can use oncreate to restore the last saved state.

 

You need to save some data when rotating the screen. There are the following methods:

1. Add Android: configchanges = "orientation | keyboardhidden" under the activity tag in the androidmanifest. xml configuration file"

Then, override the onconfigurationchanged () method in the activity. This method is called each time it is rotated. You can process data in this method [no matter how you rotate it, the current activity will not be destroyed, only the onconfigurationchanged () method is executed, so you only need to process the response data in this method.

The lifecycle is as follows:

Oncreate-> onstart-> onresume-> onconfigurationchanged ()->

Onconfigurationchanged ()

2. If the current activity is destroyed, the two methods onsaveinstancestate () and onrestoreinstancestate () of the activity must be rewritten. Obviously, the method name shows that one is to save

Data, one is to restore data, the lifecycle is as follows:

Oncreate-> onstart-> onresume-> onsaveinstancestate

-> Onpause-> onstop-> ondestroy-> oncreate-> onstart

-> Onrestoreinstancestate-> onresume

 

Activity lifecycle and status saving

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.