Android-Onsite protection, android-Onsite

Source: Internet
Author: User

Android-Onsite protection, android-Onsite
Field Protection

When an Activity enters the stopped status, it may be recycled by the system. We have learned the lifecycle of the Activity.

When the activity is in three states: onPause (), onStop (), and onDestroy (), the program may be recycled 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.

To solve this problem, Android provides the onSaveInstanceState (BundloutState) method, which is called before the program is recycled. It can be called before the program is directly called by onStop, however, it is not guaranteed whether the package is before onPause. The stored data will exist in a bundle object. This object can be passed as a parameter when the next time the program starts to call onCreate (Bundle) or onRestoreInstanceState (Bundle, this is why the onCreate method of every activity has such a section:

1 protected void onCreate (Bundle savedInstanceState) 2 {3 // TODO Auto-generated method stub4 super. onCreate (savedInstanceState); 5}OnCreate

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. Sometimes, according to user settings, my mobile phone will kill when you press the return key, and the home key will not kill.
OnSaveInstanceState (Bundle outState)
{
Super. onSaveInstanceState (outState );
You can use the put Method of outState to save data. Each save method requires two parameters, the first is the key, and the second is the value.

}

In fact, the android source code provides a very detailed explanation. Here I just wrote my own understanding of on-site protection.

 

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.