"Onsite protection" for Android apps"

Source: Internet
Author: User

When learning the operating system, we know that there is an interrupt mechanism when the cup processes transactions to facilitate transaction switching and the process of Interrupt Processing: 1) Wake up the blocked Driver (Program) process; 2) Protect the interrupted CPU environment; 3) Transfer to the response device processing program; 4) interrupt processing; 5) resume the interrupted process.

Similar concepts are also available in Android.

When the program is in three states: onpause (), onstop (), and ondestroy (), the program may be killed by the Android system, in this case, if you do not save the data before the protection operation, the user's data or modifications in the program will be lost. That is to say, "On-site protection". We hope that the last data will be restored when the program is running next time.

Android provides the onsaveinstancestate (bundle) method, which can be called before the program is onstop (), but cannot guarantee whether it is before onpause. (The original statement is as follows: if called, this method will occur before onstop (). There are no guarantees about whether it will occur before or after onpause ().)

The role of this method is officially described as follows: 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 bundle populated by this method will be passed to both ). converting to Chinese is probably called before an activity is killed. Some status data of the activity can be saved and stored in a bundle object. This object can call oncreate (bundle) in the next Startup Program) or onrestoreinstancestate (bundle) is used as the passed parameter, which is why the oncreate method that every activity overrides has such a section:

Protected void oncreate (bundle savedinstancestate)

{

// Todo auto-generated method stub

Super. oncreate (savedinstancestate );

}.

 

The bundle object in onsaveinstancestate (bundle) is the same as the one in oncreate.

When will this method be called? In fact, it is not necessarily called when the activity is destroy. When you exit the program by using the "Return key" of the navigation button, the onsaveinstancestate (bundle) is not called because it is unnecessary. (Note: There's no guarantee that onsaveinstancestate () will be called before your activity is destroyed, because there are cases in which it won't be necessary to save the State (such as when the user leaves your activity using the back key, because the user is explicitly closing the activity ). if the method is called, it is always called before onstop () and possibly before onpause ().)

So under what circumstances is there no need to call it? The two comparison charts below clearly illustrate (the two ways in which an activity returns to user focus with its State intact: either the activity is stopped, then resumed and the activity state remains intact (left), or the activity is destroyed, then recreated and the activity must restore the previous activity state (right ).)

The understanding of onsaveinstancestate (bundle) is very detailed in the official description. Here is only part of my understanding in the learning process.

Related Article

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.