Android notes: Onsaveinstancestate and onrestoreinstancestate Summary

Source: Internet
Author: User

One, method call time

    1. Onsaveinstancestate is used to save the UI state, before the activity is killed, it is usually triggered before onstop or onpause;

    2. Onrestoreinstancestate is triggered before onresume to restore the state;

    3. Activity is killed, OnCreate is called, and Onrestoreinstancestate restores the last saved information before Onresume;

    4. Activity is not killed, OnCreate will not be called, but Onrestoreinstancestate will still be called to restore the last saved information before Onresume;




Second, the method uses the scene

    1. Onsaveinstancestate () is only called when Acitivity is killed by the system. So usually onsaveinstancestate () is only suitable for storing some temporary state, while OnPause () is suitable for persisting data.

    2. The Onsaveinstancestate () method is only suitable for storing transient data, such as the state of the UI control, the value of the member variable, and not the persisted data, which should be persisted when the user leaves the current activity at OnPause () (for example, saving data to a database or file). In this case, it is important to understand that it is not suitable for storing more time-consuming data in OnPause ().

    3. Because the Onsaveinstancestate () method method is not necessarily called, it is not appropriate to persist persisted data in the method, such as inserting records into the database. The operation to save persisted data should be placed in OnPause (). If it is a permanent value, it is saved in OnPause (), and if it is large, another thread, do not block the UI thread.



Iii. Methods of Use

1. Save the bundle in the Onsaveinstancestate method:

     @Override     protected  Void onsaveinstancestate (bundle outstate)     {         super.onsaveinstancestate (outstate);         Mmapview.onsaveinstancestate (outstate);                 savestate (outstate);    }          @Override     protected void onrestoreinstancestate (Bundle  instate)     {         Super.onrestoreinstancestate (instate);         Mmapview.onsaveinstancestate (instate);                 restorestate (instate);     } 
    /**     *  after entering the three-dimensional module, the activity will be killed, where some of the state data before the kill is saved       *      *  @param  outState      */    private void savestate (bundle outstate)      {        outstate.putparcelable (HBContant.KEY_STATE_ESTATEINFO_ Json, mjson);         outstate.putparcelable (HBContant.KEY_STATE_ Estateinfo_gallery, mgalleryjson);         outstate.putparcelable ( Hbcontant.key_state_estateinfo_flash, mflashjson);         Outstate.putparcelablearraylist (hbcontant.key_state_estateinfo_video, mvideolist);         outstate.putparcelablearraylist (hbcontant.key_state_estateinfo_news,  mnewslist);          outstate.putparcelablearraylist (hbcontant.key_state_estateinfo_houselist,  mestatehouselist);     }        private void  restorestate (Bundle instate)     {    }


2. Read the saved bundle in the OnCreate or Onrestoreinstancestate method:

        if  (savedinstancestate == null)          {            / /Normal Condition             loadtask ();         }        else         {            // Go to the three-dimensional diagram returned after Kill, restore page data             mJson =  Savedinstancestate.getparcelable (Hbcontant.key_state_estateinfo_json);             mgalleryjson = savedinstancestate.getparcelable (HBContant.KEY_ State_estateinfo_gallery);             mflashjson  = savedinstancestate.getparceLable (Hbcontant.key_state_estateinfo_flash);             mvideolist = savedinstancestate.getparcelablearraylist (Hbcontant.key_state_estateinfo_video);             mNewsList =  Savedinstancestate.getparcelablearraylist (hbcontant.key_state_estateinfo_news);             mEstateHouseList =  Savedinstancestate.getparcelablearraylist (hbcontant.key_state_estateinfo_houselist);                                      if (mJson != null  && mGalleryJson != null && mFlashJson != null  && mvideolist != null && mestateHouselist != null)             {                 loadcomplete ();             }             else             {                 Loadtask ();            }         }














This article from the "Sky no traces but I flew" blog, please be sure to keep this source http://glblong.blog.51cto.com/3058613/1432616

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.