Android Onsaveinstance method

Source: Internet
Author: User

Why use the activity state to save, how to use?

1) We hope that the information in the current activity will not be lost due to changes in activity status. For example, the switch between the screen, suddenly a phone call.

2)

Use activity Lifecycle + preference to complete the State preservation; (not detailed here)

Complete state saving with Onsaveinstancestate & OnCreate or onrestoreinstancestate;

When will the Onsaveinstancestate method be executed?

The Onsaveinstance method is called before the system reclaims activity, and the back key is not called.

1. When the user presses the home button.

It is obvious that the system does not know how many other programs to run after you press home, and it is not known whether activity A will be destroyed, so the system will call Onsaveinstancestate, giving users the opportunity to save some non-permanent data. The following are some of the conditions in which the analysis follows this principle


2. Press and hold the home key, and choose to run other programs.


3, press the power button (turn off the screen display).


4. When starting a new activity from activity a.


5, the screen direction when switching, such as from the vertical screen when switching to a horizontal screen.

Activity A is destroyed before the screen is switched on, and the system automatically creates activity a after the screen switch, so onsaveinstancestate is bound to be executed

In summary, Onsaveinstancestate's invocation follows an important principle that when the system destroys your activity "without your permission", then Onsaveinstancestate is called by the system, which is the responsibility of the system, Because it has to provide an opportunity for you to save your data (of course you don't save it then just do it yourself).


As for the Onrestoreinstancestate method, it should be noted that the Onsaveinstancestate method and the Onrestoreinstancestate method "not necessarily" are called in pairs, Onrestoreinstancestate is invoked only if activity a is "really" destroyed by the system, and if it is just stuck in a situation where there is such a possibility, then the method will not be called, for example, when activity A is being displayed, The user presses the home key back to the main interface, and then the user returns to activity a, in which event A is generally not destroyed by the system for memory reasons, so activity A's Onrestoreinstancestate method will not be executed.

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.