When an activity becomes "easy" to be destroyed by the system, the activity's onsaveinstancestate is executed unless the activity is actively destroyed by the user, such as when the user presses the back key.
Note The double quotes above, what is "easy"? The implication is that the activity has not been destroyed, but merely a possibility. What are the possibilities? By overriding the OnXxx method of all life cycles of an activity, including the Onsaveinstancestate and Onrestoreinstancestate methods, we can clearly know when an activity (assumed activity A)
1. When the user presses the home button.
It is obvious that the system does not know how many other programs to run after you press home, and it is not known whether activity A will be destroyed, so the system will call Onsaveinstancestate, giving users the opportunity to save some non-permanent data . The following are some of the conditions in which the analysis follows this principle
2. Press and hold the home key, and choose to run other programs.
3, press the power button (turn off the screen display).
4. When starting a new activity from activity a.
5, the screen direction when switching, such as from the vertical screen when switching to a horizontal screen.
Activity A is destroyed before the screen is switched on, and the system automatically creates activity a after the screen switch, so onsaveinstancestate is bound to be executed
In summary, Onsaveinstancestate's invocation follows an important principle that when your activity is destroyed by the system "without your permission", then Onsaveinstancestate will be called by the system, which is the responsibility of the system. Because it has to provide an opportunity for you to save your data (of course you don't save it then just do it yourself).
public void Onsaveinstancestate (Bundle outstate) { super.onsaveinstancestate (outstate); outstate.putlong ("id", 1234567890); //save}
B after completion will come to find a, this time there are two situations, one is a is recycled, one is not recycled, the recovery of a must recall the OnCreate () method, different from the direct start is this back OnCreate () is with the parameter savedinstancestate, It's better to be onresume without being taken back.
savedinstancestate is a bundle object, you can basically understand him as a system to help you maintain a map object. In OnCreate () you may use it, if the normal start oncreate will not have it, so use to determine whether it is empty.
if (savedinstancestate! = null) {Long id = savedinstancestate.getlong ("id"); }
Activity A can restore the state of the interface through OnCreate (bundle) or onrestoreinstancestate (bundle). Onrestoreinstancestate is invoked on the premise that activity a "does" have been destroyed by the system, and that the method will not be called if it is just stuck in the case where there is such a possibility.