Save activity's State

Source: Internet
Author: User

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

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.