Method onsaveinstancestate (bundle) and onrestoreinstancestate (bundle) of the activity )).
Protected voidOnsaveinstancestate (BundleOutstate)
Called to retrieve per-instance state from an activity before being killed so that the State can be restored in oncreate (bundle)Or onrestoreinstancestate (bundle)(The bundlePopulated by this method will be passed to both ).
This method is called before an activity may be killed so that when it comes back some time in the future it can restore its State. for example, if activity B is launched in front of Activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity, the state of the user interface can be restored via oncreate (bundle)Or onrestoreinstancestate (bundle).
Do not confuse this method with activity lifecycle callbacks such as onpause (), Which is always called when an activity is being placed in the background or on its way to destruction, or onstop ()Which is called before destruction. One example of when onpause ()And onstop ()Is called and not this method is when a user navigates back from Activity B to activity A: there is no need to call onsaveinstancestate (bundle)On B because that participant instance will never be restored, so the system avoids calling it. An example when onpause ()Is called and not onsaveinstancestate (bundle)Is when Activity B is launched in front of Activity A: The system may avoid calling onsaveinstancestate (bundle)On activity a if it isn' t killed during the lifetime of B since the State of the user interface of a will stay intact.
The default implementation takes care of most of the UI per-instance state for you by calling onsaveinstancestate ()On each view in the hierarchy that has an ID, and by saving the ID of the currently focused view (all of which is restored by the default Implementation of onrestoreinstancestate (bundle)). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself.
If called, this method will occur before onstop (). There are no guarantees about whether it will occur before or after onpause ().
If onsaveinstancestate is to be called, it must be called before stop. But it can be called before or after onpause.
Note: it should be said that the function is called only when active is killed. However, pressing the back key is not called.End active with finish ()Nor will onsaveinstancestate be called. Since it cannot be reborn after destruction, there is no need to call onsaveinstancestate to saveProtected
Void
Onrestoreinstancestate (Bundle Savedinstancestate) This method is called after onstart () When the activity is being re-initialized from a previusly saved state, given here in savedinstancestate. Most implementations will simply use oncreate (bundle) To restore their State, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. the default implementation of this method performs a restore of any view State that had previously been frozen by onsaveinstancestate (bundle) . This method is called between onstart () And onpostcreate (bundle) .
Note 1: The savedinstancestate is in The stored bundle is reported in onsaveinstancestate.
NOTE 2: it can also be read in oncreate. In The stored bundle is reported in onsaveinstancestate. However Onrestoreinstancestate Processing is also convenient. At least look good.