The function of saveinstancestate parameter in OnCreate method

Source: Internet
Author: User

Example code:

public class Mainactivity extends Actionbaractivity {private static final String TAG = "mainactivity";        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        if (null! = savedinstancestate) {int inttype = Savedinstancestate.getint ("Inttype");        String StringType = savedinstancestate.getstring ("StringType");        } setcontentview (R.layout.activity_main);            LOG.I (TAG, "onCreate----performed--------"); } @Override protected void Onrestoreinstancestate (Bundle savedinstancestate) {super.onrestoreinstancestate (s    Avedinstancestate);    int inttype = Savedinstancestate.getint ("Inttype");    String StringType = savedinstancestate.getstring ("StringType");    LOG.I (TAG, "onrestoreinstancestate----performed--------");    } @Override protected void Onsaveinstancestate (Bundle outstate) {super.onsaveinstancestate (outstate);    Outstate.putint ("Inttype", 0); Outstate.putstring ("StrinGType "," saved ");    LOG.I (TAG, "onsaveinstancestate----performed--------"); }}

When will the Onsaveinstancestate method be executed?

1. When the user presses the home key.

It is obvious that the system does not know how many other programs you want to run after you press home, and it is not known whether acitvity a will be destroyed so the system will call Onsaveinstancestate, giving the user the opportunity to save some non-permanent data, following several analyses that follow this principle.

2. Press and hold the home key to select another program to run.

3. Press the Power key (turn off the screen display).

4. When a new activity is initiated from activity a.

5. When the screen orientation is switched.


When will the Onrestoreinstancestate method be executed?

Activity a "really" is destroyed by the system, and if only stay in the case of this possibility, then the method will not be called, for example, when the Acitivty A is being displayed, the user presses the home button back to the main interface, and then the user next to return to activity a, In this case, activity A is not normally destroyed by the system for memory reasons, so activity A's Onrestoreinstancestate method will not be executed.

The function of saveinstancestate parameter in OnCreate method

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.