"Go" Android Notes-save and restore activity's status data

Source: Internet
Author: User

In general, the activity instances after calling the OnPause () and OnStop () methods still exist in memory, and all information and state data for the activity will not disappear, and all changes will be preserved when the activity returns to the foreground.

However, when the system is out of memory, the activity after calling the OnPause () and OnStop () methods may be destroyed by the system, where the activity's instance object is not present in memory. If this activity goes back to the foreground, the changes made will disappear. To avoid this, developers can overwrite the Onsaveinstancestate () method. The Onsaveinstancestate () method accepts a bundle type parameter, and the developer can store the state data in the bundle object, so that even if the activity is destroyed by the system, When the user restarts the activity and calls its OnCreate () method, the bundle object described above is passed as an argument to the OnCreate () method, and the developer can remove the saved data from the bundle object. This data is then used to restore activity to the state before it was destroyed.

Java code
 Public classMainactivityextendsActivity { Public Static Final intsecond_activity = 0; PrivateString temp; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //recover data from savedinstancestate if no data is required to restore savedinstancestate to null        if(Savedinstancestate! =NULL) {Temp= savedinstancestate.getstring ("temp"); System.out.println ("Oncreate:temp =" +temp); }      }         Public voidOnresume () {Super. Onresume (); Temp= "Xing"; System.out.println ("Onresume:temp =" +temp); //Switching the screen orientation will cause the activity to be destroyed and rebuilt        if(getrequestedorientation () = =activityinfo.screen_orientation_unspecified)              {setrequestedorientation (Activityinfo.screen_orientation_landscape); System.out.println ("Screen Toggle"); }      }            //saves the data to the Outstate object, which is passed to the OnCreate method when the activity is rebuilt@Overrideprotected voidonsaveinstancestate (Bundle outstate) {Super. Onsaveinstancestate (outstate); Outstate.putstring ("Temp", temp); }  }

It is important to note that the Onsaveinstancestate () method is not necessarily called, because some scenarios do not need to save state data. For example, when the user presses the back key to exit the activity, the user obviously wants to close the activity, at which point there is no need to save the data for the next recovery, i.e. the Onsaveinstancestate () method will not be called. If the Onsaveinstancestate () method is called, the call occurs before the OnPause () or OnStop () method.

Default implementation of the Onsaveinstancestate () method

If the developer does not overwrite the Onsaveinstancestate () method, the default implementation of this method automatically saves some state data in the activity, such as the state of the various UI controls in the activity. Almost all UI controls defined in the Android application framework implement the Onsaveinstancestate () method appropriately, so these UI controls automatically save and restore state data when activity is destroyed and rebuilt. For example, the EditText control automatically saves and restores the input data, and the CheckBox control automatically saves and restores the selected state. Developers just need to specify a unique ID for these controls (by setting the Android:id property), and the rest can be done automatically. If you do not specify an ID for the control, the control does not perform automatic data saving and recovery operations.

As mentioned above, if the developer needs to overwrite the Onsaveinstancestate () method, the default implementation of the method is typically called in the first line of code: Super.onsaveinstancestate (outstate).

Whether you need to overwrite the Onsaveinstancestate () method

Since the default implementation of the method can automatically save the UI control's state data, when do you need to overwrite the method?

If you need to save additional data, you need to overwrite the Onsaveinstancestate () method. If you need to save the value of the member variable in the class (see the previous example).

Onsaveinstancestate () method is suitable for storing what data

Because the Onsaveinstancestate () method method is not necessarily called, it is not appropriate to persist persisted data in the method, such as inserting records into the database. The operation to save persisted data should be placed in OnPause (). The Onsaveinstancestate () method is only suitable for storing transient data, such as the state of a UI control, the value of a member variable, and so on.

Other situations that trigger activity destruction and reconstruction

In addition to the fact that the system is out of memory to destroy activity, changes in some system settings can cause activity to be destroyed and rebuilt. For example, change the screen orientation (see the example above), Change device language settings, keyboard popup, etc.

from:http://coolxing.iteye.com/blog/1279447

"Go" Android Notes-save and restore activity's status data

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.