1. Save data under the normal end activity (back key and Finish ()):
OnPause (): Used to save data operations, Reason: OnPause () and Onresume () are bound to be executed in the activity. OnStop () is not necessarily executed.
2. The system is not running out of memory, resulting in the recovery (i.e. destruction) of the activity or screen rotation of the current save data:
Onsaveinstancestate (): Used to save data. The saved data can be the data entered in the EditText, and the item in the ListView is slid to the specified location.
OnCreate (): Used to get the saved data. However, you must determine whether the parameter is zero or the null pointer exception may be reported
Onrestoreinstancestate (): Used to get the saved data, you can not determine whether the parameter is zero.
Note: The screen rotation is that the system destroys the current activity and then re-creates a new activity (that is, restarting the destroyed activity).
(Prerequisite: Due to insufficient memory, the system recycles activity that is not at the top of the stack.) That is, the method will not be used when the user normally ends with the back key and finish finishes.
The system calls this method to save the state of the activity when the current activity starts stop, if the activity is not normally destroyed.
This is typically used to save the input in EditText, or position is selected in the ListView
protected void Onsaveinstancestate (Bundle outstate) {
Outstate.putstring ("Onsaveintancestate", s);
Outstate.putint ("position", 1);
Super.onsaveinstancestate (outstate);
LOG.I ("Mainactivity", "onsaveinstancestate");
}
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
edittext= (EditText) Findviewbyid (R.ID.EDITTEXT1);
tv= (TextView) Findviewbyid (R.ID.TEXTVIEW1);
if (savedinstancestate!=null) {
Save1=savedinstancestate.getstring ("Onsaveintentstate");
int I=savedinstancestate.getint ("position");
Edittext.settext (SAVE1);
Tv.settext (i);
}
LOG.I ("Mainactivity", "OnCreate" +savedinstancestate);
S=edittext.gettext (). toString ();
}
After the current activity starts the OnStart () method, the system calls the method to restore the last state.
Compared to the OnCreate () method, it is not necessary to determine whether the parameter savaeinstancestate is null
protected void Onrestoreinstancestate (Bundle savedinstancestate) {
String s=savedinstancestate.getstring ("onsaveintentstate");
int I=savedinstancestate.getint ("position");
Super.onrestoreinstancestate (savedinstancestate);
LOG.I ("Mainactivity", "onrestoreinstancestate");
}
Save activity's State