Android training-rebuilding an activity

Source: Internet
Author: User

This article translated from: http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Due to some common application behaviors, sometimes the activity will be destroyed, for example, when the user presses the back button or calls the finish () method to send a self-destruction signal. If the activity is terminated and is not used for a long time, or the foreground activity requires more resources, the system must turn off the background process to recycle the memory, the system may also destroy your activity.

When a user presses the back button or calls the finish () method of the activity to be destroyed, the system will think that the instance of the activity is gone forever, this action indicates that this activity is not required. However, if the activity is destroyed (rather than normal behavior) because of system force, the system will remember its existence even though the actual activity instance has disappeared, if the user returns the activity again, the system uses a group of State data that describes the activity saved when it is destroyed to recreate the activity. The stored data used by the system to restore the previous state is called "instance status" and is a set of key-value pairs stored in the bundle object.

Warning your activity will be destroyed and rebuilt every time you rotate the screen. When the screen changes, the system will destroy and recreate the foreground activity because the screen configuration changes and your activity may need to load other resources (such as layout.

By default, the system uses the bundle instance to save the status information of each view object in the activity layout (such as the text value already typed in the text field ). Therefore, if your activity instance is destroyed and rebuilt, you do not need to write any code to restore the previous layout. However, your activity may need to restore more status information, such as member variables that track the progress of users in the activity.

Note: In order for the Android system to restore the status of all views in the activity, each view object must have a unique ID provided by the Android. ID attribute.

To save data outside the activity state, you must override the onsaveinstancestate () callback method. When a user leaves your activity, the system will call this method and pass the data you want to save to the bundle object, so that the data will be saved when the activity is destroyed. If the system must recreate the activity in the future, the system will pass the bundle object to the onrestoreinstancestate () and oncreate () methods.

Figure 1. when the system starts to terminate your activity, it will call the onsaveinstancestate () (1) method, therefore, you can specify the status data that you want to restore when rebuilding out of quota in this callback method. If the activity is destroyed and the same instance must be rebuilt, the system will pass the status data saved in (1) to the oncreate method (2) and onrestoreinstancestate () method (3 ).

Save activity status

When your activity starts to terminate, the system will call the onsaveinstancestate () method. Therefore, your activity status can save the status information in a key-value pair set. By default, this method saves the status information about the view hierarchy in the activity, such as text in the text field and the position of the listview scroll bar.

To save additional information about your activity, you must implement the onsaveinstancestate () method and add the key-value pair to the bundle object, for example:

Static final string state_score = "playerscore ";

Static final string state_level = "playerlevel ";

...

 

@ Override

Public void onsaveinstancestate (bundle savedinstancestate ){

// Save the user's current game state

Savedinstancestate. putint (state_score, mcurrentscore );

Savedinstancestate. putint (state_level, mcurrentlevel );

// Always call the superclass so it can save the view hierarchy state

Super. onsaveinstancestate (savedinstancestate );

}

Warning always call the superclass implementation of the onsaveinstancestate () method so that the default implementation can save the state of the view hierarchy.

Restore activity status

When your activity is to be rebuilt from the previously destroyed state, you can restore the Saved state from the bundle object passed to your activity by the system. Both the oncreate () and onrestoreinstancestate () Methods restore the instance status information contained in the same bundle object.

The oncreate () method is called to determine whether the system creates a new activity instance or restores an activity instance from the previous State. Therefore, before trying to read information from the bundle object, you must check whether the bundle object is null. If it is null, the system will create a new activity instance instead of restoring it from the previously destroyed state.

For example, the following code demonstrates how to include status data in the oncreate () method:

@ Override

Protected void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate); // always call the superclass first

// Check whether we're re recreating a previusly destroyed instance

If (savedinstancestate! = NULL ){

// Restore value of members from Saved state

Mcurrentscore = savedinstancestate. getint (state_score );

Mcurrentlevel = savedinstancestate. getint (state_level );

} Else {

// Probably initialize members with default values for a new instance

}

...

}

Unlike the restoration status during oncreate () method execution, you can choose to implement the onrestoreinstancestate () method, which will be called by the system after the onstart () method. The system calls the onrestoreinstancestate () method only when the stored data is to be restored. Therefore, you do not need to check whether the bundle object is null:

Public void onrestoreinstancestate (bundle savedinstancestate ){

// Always call the superclass so it can restore the view hierarchy

Super. onrestoreinstancestate (savedinstancestate );

// Restore state members from saved instance

Mcurrentscore = savedinstancestate. getint (state_score );

Mcurrentlevel = savedinstancestate. getint (state_level );

}

Warning always call the superclass implementation of the onrestoreinstancestate () method to restore the view level by default.

 

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.