Functions of onsaveinstancestate function and Onrestoreinstancestate function
onsaveinstancestate Function: The state information (UI control state information) used to hold the activity and the information saved by the userOnrestoreinstancestate function: Used to restore state information and user-saved information when an activity is destroyed by the system
second, the call time of the Onsaveinstancestate function
1. When the user presses the home key
2, long press the Home button, choose to run other programs
3, press the power button (turn off the screen display)4. Starting a new activity from activity a
5, when the screen direction switch, for example, from the vertical screen switch to the horizontal screen
When the system destroys an activity, the Onsaveinstancestate method is invoked, such as insufficient memory, the user presses the home key, etc., and when the user presses the return key, the Onsaveinstancestate method is not invoked. Because the system doesn't think it's necessary to save the data. The default implementation of this method automatically saves certain state data in the activity, such as the state of various UI controls in the activity, if we do not have the Onsaveinstancestate () method. If we need to overwrite the Onsaveinstancestate () method, we typically call the default implementation of the method in the first line of code: Super.onsaveinstancestate (outstate)
principles of data storage and recovery dataWhen the screen direction is switched, the activity is reset by the system, and the Onsaveinstancestate function is called to save the data, and the OnCreate () method and Onrestoreinstancestate () are called after the screen switch is complete. Method method to recover the data, in the following order: The Onsaveinstancestate function is called to save the state information of the activity before the activity is destroyed (savehierarchystate and saveallstate) and user-saved data call OnCreate () method restoreallstate, restore Allstate All state information call Onrestoreinstancestate () Method Restorehierarchystate Restore Class Call information how do we know this order, and also from the activity lifecycle and use Logcat to see the following logcat data, for switching the screen direction of the Logcat information
As you can see, the order in which the functions are called when you switch the screen is OnPause-->onsaveinstancestate-->onstop-->ondestory-->oncreate ( OnCreate method to invoke when recreating an activity after switching the screen-->onstart-->onrestoryinstancestate-->onresume according to the Android official source code, The Onsaveinstancestate method must be called before the OnStop method, but it is not guaranteed to be invoked before or after the OnPause method, as explained in the official source code: If called, this methods will occur beforeonstop. There are no guarantees about whether it'll occur before or afteronpause. What exactly did you save when invoking the Onsaveinstancestate method to save activity state data? See the source code for the Onsaveinstancestate function.
protected void Onsaveinstancestate (Bundle outstate) {
outstate.putbundle (Window_hierarchy_tag, Mwindow.savehierarchystate ());
Parcelable p = mfragments.saveallstate ();
if (P!= null) {
outstate.putparcelable (Fragments_tag, p);
}
Getapplication (). Dispatchactivitysaveinstancestate (this, outstate);
}
From the above Android source can be seen, in fact, is also saved to the bundle, save the data there are two, window hierarchystate, can be understood as a window of the class state, which contains the data has been printed in the picture above, in fact, is also a number of panels , some UI controls that contain IDs, and some actionbar and other information, including information that is saved by the user himself, which is later used to save all the states of fragment with a Paracelable object. The OnCreate method was invoked when the Paracelable object was added to the bundle object to recover data, oncreate the method source code as follows
protected void OnCreate (Bundle savedinstancestate) {if (debug_lifecycle) slog.v (TAG, "Oncre
Ate "+ This +": "+ savedinstancestate);
if (mlastnonconfigurationinstances!= null) {mallloadermanagers = mlastnonconfigurationinstances.loaders; } if (Mactivityinfo.parentactivityname!= null) {if (Mactionbar = null) {MEna
Bledefaultactionbarup = true;
else {mactionbar.setdefaultdisplayhomeasupenabled (true); } if (savedinstancestate!= null) {parcelable p = savedinstancestate.getparcelable (FRAGMENT
S_tag); Mfragments.restoreallstate (p, mlastnonconfigurationinstances!= null? mlastnonconfigurationinstances.
Fragments:null);
} mfragments.dispatchcreate ();
Getapplication (). dispatchactivitycreated (this, savedinstancestate);
Mcalled = true; }
From the above source code can be seen, the OnCreate method to restore the data is to restore the Paracelable object under the Fragment Allstate, the premise is saveinstancestate!=null, this condition is generally not easy to meet, The parameter Saveinstancestate in the OnCreate method is not empty until the activity is destroyed by the system, otherwise, even if the Onsaveinstancestate method is invoked, The parameter Saveinstancestate in the OnCreate method is also still null, which can be derived from comments in the source code Parameters:
savedinstancestateIf the activity was being re-initialized after previously being shut down then this Bundle contains the data it most recent Ly supplied in Onsaveinstancestate.
note:otherwise it is null
After calling the OnCreate method, the system then calls the Onrestoryinstancestate method to recover the data, and the source code for the Onrestoryinstancestate method is as follows:
protected void Onrestoreinstancestate (Bundle savedinstancestate) {
if (Mwindow!= null) {
Bundle WindowState = sa Vedinstancestate.getbundle (Window_hierarchy_tag);
if (WindowState!= null) {
mwindow.restorehierarchystate (windowstate);}}}
From the source above, it can be seen that the Onrestoryinstancestate function restores the data is hierarchystate again the data saved in the Onsaveinstancestate method, one of which is the ID of the UI control, Not all UI control information is saved and must meet two criteria the UI control has an ID the UI space is currently focused, that is, the two conditions that are clicked can also be drawn from the comments in the source code, and you can write a demo to prove that this explanation is true to the default Implementation takes care of most of the UI per-instance state for you by calling Android.view.View.onSaveInstanceState () On the hierarchy that has a ID, and by saving the ID of the currently focused view (this is which By the default implementation ofonrestoreinstancestate).
This can be summed up: When the activity is reset by the system, the sequence of function calls to save and restore the data is: Onsaveinstancestate (save data)-->oncreate (Restore data Allstate)--> Onrestoryinstancestate (Restore data hierarchystate) The data saved by the Onsaveinstancestate function: View.hierarchystate, that is, the Panels,ui control, Actionbar, etc.
The saved UI control must be an ID, and the UI control that is currently gaining focus will be saved