One, method call time
Onsaveinstancestate is used to save the UI state, before the activity is killed, it is usually triggered before onstop or onpause;
Onrestoreinstancestate is triggered before onresume to restore the state;
Activity is killed, OnCreate is called, and Onrestoreinstancestate restores the last saved information before Onresume;
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
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.
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 ().
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