API 21 adds a new attribute to the activity, as long as it is set to persistacrossreboots,activity and has the ability to persist, in addition to a new bundle, which is persistablebundle.
The persistence here differs from the traditional meaning of the onsaveinstancestate, Onrestoreinstancestate, and OnCreate methods that are specifically implemented in the activity overload.
publicvoidonSaveInstanceState(Bundle outState, PersistableBundle outPersistentState)publicvoidonRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState)publicvoidonCreate(Bundle savedInstanceState, PersistableBundle persistentState)
The Onsaveinstancestate and Onrestoreinstancestate methods are a way to save the disaster, they are not in the "normal" activity life cycle, only some unexpected anomalies will trigger them, such as the screen to switch, press the home button. After API 21, the Persistablebundle parameter is added, so that these methods have the ability to recover data after the system shutdown restart.
The comments were different, but in any case this provided us with a convenience. The scenario in which it is applied is an abnormal condition that does not affect our normal data persistence approach. For example, in the pause method to do some operations preferences, file I/o,sqlite database, ContentProvider and other conventional methods.
How to practice it?
Just set the properties in the activity in manifest:
android:persistableMode="persistAcrossReboots"
You can then use the above three methods directly in the activity.
Also note that the API version is 21 and above.
Validation is a challenge. Because I do not have 5.0 and above system equipment, help and simulator bar, all kinds of problems come. Whether it is a genymotion or a self-contained simulator, the simulator will die during shutdown. Although I saw in log that the program has gone through the Onsaveinstancestate (Bundle,persistablebundle). When the simulator is turned off, look at the effect. My side is not successful, if any prawn see the effect, please tell me.
Android Combat 26: Persistablemode and activity persistence