Android Combat 26: Persistablemode and activity persistence

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.