Use of Onsaveinstancestate and onrestoreinstancestate in Android activity

Source: Internet
Author: User

Onsaveinstancestate () and Onrestoreinstancestate () methods for Android activity:

1. Basic role:

Activity's Onsaveinstancestate () and onrestoreinstancestate () are not life cycle methods, they are not necessarily triggered, unlike life cycle methods such as onCreate (), OnPause (). Onsaveinstancestate () is called when an activity is destroyed by the system when an application encounters an unexpected condition (such as insufficient memory, the user presses the home key directly). However, when the user actively destroys an activity, such as pressing the return key in the app, Onsaveinstancestate () is not called. Because in this case, the behavior of the user determines that there is no need to save the activity State. usually Onsaveinstancestate () is only suitable for storing some temporary state, while OnPause () is suitable for persisting data.

The state of each instance is saved before the activity is killed to ensure that the state can be in OnCreate (bundle) or Onrestoreinstancestate (bundle) ( The incoming bundle parameters are recovered from the Onsaveinstancestate package. This method is called before an activity is killed and can revert to its previous state when the activity returns at some point in the future.

For example, if ACTIVITYB is enabled on the front end of Activitya, at some point Activitya because the system recycles the resource to be killed, a through onsaveinstancestate will have the opportunity to save its user interface state, Enables future users to return to Activitya by OnCreate (bundle) or onrestoreinstancestate (bundle) to restore the state of the interface.

About Onsaveinstancestate () is to save some view useful data to a Parcelable object and return it inside the function. Calls the view's Onsaveinstancestate () in the Onsaveinstancestate (Bundle outstate) of the activity, returns the Parcelable object,

The bundle's Putparcelable method is then used to store the bundle Savedinstancestate.

When the system calls the activity's onrestoreinstancestate (bundle savedinstancestate), the Getparcelable method of the same bundle gets the Parcelable object, The Parcelable object is then passed to the view's Onrestoreinstancestate (parcelable state). Read the saved data from the parcelable in the view's onrestoreinstancestate for view use.

This is the basic function and usage of the two functions of onsaveinstancestate () and Onrestoreinstancestate ().

2. Onsaveinstancestate () when to call

First look at application fundamentals on a passage:

Android calls Onsaveinstancestate () before the activitybecomes vulnerable to being destroyed by the system, but does not B Othercalling it when the instance are actually being destroyed by a user action (Suchas pressing the back key).

As you can tell, when an activity becomes "easy" to be destroyed by the system, the activity's onsaveinstancestate () is executed unless the activity is actively destroyed by the user, such as when the user presses the back key.

Notice the double quotes above, what is "easy"? It means that the activity has not been destroyed, but only a possibility. What are the possibilities? By overriding the OnXxx method of all life cycles of an activity, including the Onsaveinstancestate () and Onrestoreinstancestate () methods, We can clearly see when an activity (assumed to be Activitya) is displayed at the top of the current task, when its onsaveinstancestate () method is executed, in several cases:

(1), when the user presses the home button.

It is obvious that the system does not know how many other programs to run after you press home, and it is not known whether activity A will be destroyed, so the system will call Onsaveinstancestate (), giving the user the opportunity to save some non-permanent data. The following are some of the conditions in which the analysis follows this principle

(2), long press the home key, choose to run other programs.

(3), Press the power button (turn off the screen display).

(4), when starting a new activity from activity a.

(5), the screen direction when switching, such as from the vertical screen when switching to a horizontal screen.

Before the screen is switched on, activity A is destroyed, and activity A is automatically created after the screen switch, so onsaveinstancestate () is bound to be executed and will certainly execute onrestoreinstancestate ().

All in all, the invocation of Onsaveinstancestate () follows an important principle that onsaveinstancestate () will be called by the system when there is a possibility of destroying our activity "without your permission", which is the responsibility of the system, Because it has to provide an opportunity for you to save your data (of course you don't save it then just do it yourself). If called, the call occurs before the OnPause () or OnStop () method. (although most of the tests were found before OnPause ())

3. Onrestoreinstancestate () when to call

Onrestoreinstancestate () is invoked on the premise that activity a "does" have been destroyed by the system, and that the method will not be invoked if it is merely stuck in the case where activity A is being displayed, for example, The user presses the home key back to the main interface, and then the user then returns to activity a, in which event A is not normally destroyed by the system for memory reasons, so activity A's Onrestoreinstancestate method is not executed This also shows that the two, in most cases, are not paired to be used.

Onrestoreinstancestate () is called between OnStart () and Onpostcreate (Bundle).

4. The default implementation of the Onsaveinstancestate () method

If we do 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 only need to specify a unique ID for these controls (by setting the Android:id property), The rest can be done automatically. If no ID is specified for the control, the control does not perform automatic data saving and recovery operations.

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

5. Whether you need to override 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. It is important to note that the Onsaveinstancestate () method is only suitable for storing transient data, such as the state of the UI control, the value of the member variable, and not the persisted data, which should be persisted when the user leaves the current activity at OnPause () (for example, saving data to a database or file). In this case, it is important to understand that it is not suitable for storing more time-consuming data in OnPause ().

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 (). If it is a permanent value, it is saved in OnPause (), and if it is large, another thread, do not block the UI thread.

6. Other circumstances 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.

Use of Onsaveinstancestate and onrestoreinstancestate in Android activity

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.