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.