Android: the timing of onsaveinstancestate and onrestoreinstancestate triggering

Source: Internet
Author: User

Let's take a look at a passage on Application Fundamentals:

 

Android callonsaveinstancestate () before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a user action (such as pressing the back key)

 

From this sentence, we can know that when an activity becomes "easy" to be destroyed by the system, the onsaveinstancestate of the activity will be executed, unless the activity is manually destroyed by the user, for example, when you press the back key.

Pay attention to the double quotation marks above. What is "easy "? The implication is that the activity has not been destroyed, but it is only a possibility. What are the possibilities? By Rewriting the onxxx method of all life cycles of an activity, including the onsaveinstancestate and onrestoreinstancestate methods, we can clearly know when an activity (assumed activity) when the onsaveinstancestate method of the current task is displayed at the top layer, the onsaveinstancestate method will be executed in the following situations:


1. When the user presses the Home key.

This is obvious. The system does not know how many other programs you want to run after you press home, and naturally does not know whether activity A will be destroyed. Therefore, the system will call onsaveinstancestate, this gives users the opportunity to save some non-permanent data. This principle is applicable to the following situations:


2. Press the Home Key and select to run other programs.


3. Press the power button to close the screen.


4. Start a new activity from activity.


5. When switching the screen direction, for example, switching from the portrait screen to the landscape screen.

Before the screen switch, Activity A is destroyed, and activity A is automatically created after the screen switch. Therefore, onsaveinstancestate will be executed.

 

All in all, onsaveinstancestate calls follow an important principle, that is, when the system destroys your activity "without your permission", the onsaveinstancestate will be called by the system, which is the responsibility of the system, because it must provide an opportunity for you to save your data (of course, if you do not save it, you just need ).

 


As for the onrestoreinstancestate method, it should be noted that the onsaveinstancestate method and the onrestoreinstancestate method are not necessarily called in pairs, and the onrestoreinstancestate is called on the premise that, activity A is indeed destroyed by the system. If it is only possible to stay there, the method will not be called. For example, when activity A is being displayed, the user presses the Home Key to return to the main interface, and then the user returns to activity A. In this case, Activity A is generally not destroyed by the system because of memory, so the onrestoreinstancestate method of activity a will not be executed.


In addition, the bundle parameter of onrestoreinstancestate will be passed to the oncreate method. You can also choose to restore data in the oncreate method.

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.