Android official Getting Started document [15] recreate an activity event

Source: Internet
Author: User
Tags unique id

Android official Getting Started document [15] recreate an activity event
Recreating an Activity
Re-create an activity event

This lesson teaches
1.Save Your Activity State
2.Restore Your Activity State

You should also read
? Supporting Different Screens
? Handling Runtime Changes
? Activities

This lesson teaches you
1. Save your Activity status
2. Restore your Activity status

You should also read
? support for different screens
? handling changes in run time
? Activity activities

there is a few scenarios in which your activity was destroyed due to normal app behavior, such as when the user presses the "Back button" or your activity signals its own destruction by calling finish (). The system may also destroy your activity if it's currently stopped and hasn ' t been used in a long time or the foreground Activity requires more resources so the system must shut down background processes to recover memory.
There are cases where activity activities are corrupted due to normal application behavior, when the user presses the return button or activity by calling the finish signal itself to destroy () such as. The system can also destroy your activity if it is currently stopped and is not being used for a long time or the foreground activity requires more resources, so the system must shut down the background process to recover memory.

When your activity was destroyed because the user presses back or the activity finishes itself, the system ' s concept of tha T activity instance is gone forever because the behavior indicates the activity is no longer needed. However, if the system destroys the activity due to system constraints (rather than normal app behavior) and then although th E actual Activity instance is gone, the system remembers that it existed such so if the user navigates back to it, the S Ystem creates a new instance of the activity using a set of saved data that describes the state of the activity when it WA S destroyed. The saved data, the system uses to restore the previous state was called the "instance state" and is a collection of Ke Y-value pairs stored in a Bundle object.
The concept of the system for the activity instance is gone, because the behavior indicates that the activity is no longer needed, because the activity of the user pressing the return or activities completion itself is destroyed. However, if the system destroys the activity due to system limitations (rather than normal application behavior), then while the actual activity instance is gone, the system remembers that it exists so that if the user navigates back to it, the system creates a new example using a set of stored data descriptions, Activity activities when it is destroyed by the activity state. The system uses a collection of key-value pairs that revert to the previous state of the saved data called "instance state" and stored in a bundle object.

Caution:your activity is destroyed and recreated each time the user rotates the screen. When the screens changes orientation, the system destroys and recreates the foreground activity because the screen Configur ation have changed and your activity might need to load alternative resources (such as the layout).
Note: Your activity will be destroyed as each user rotates the screen while rebuilding. When the screen changes direction, the system destroys and re-creates the foreground activity because the configuration on the screen has changed and your activity may need to load alternative resources (such as layouts).

By default, the system uses the Bundle instance state to save information on each View object in your activity layout ( such as the text value entered into an EditText object). So, if your activity instance are destroyed and recreated, the state of the layout is restored to its previous state with N o Code required by you. However, your activity might has more state information so you ' d like to restore, such as member variables so track T He user ' s progress in the activity.
By default, the system uses the Bundel bundle instance state to hold information about each view object in your activity layout (such as entering the text value of a EditText object). So, if your activity instance is destroyed, rebuilt, the state of the layout reverts to the previous state, not the code you need. However, your activity may have more state information about the progress member variables that you want to recover, such as tracking activity for the user.

Note:in order for the Android system to restore the state of the your activity, each view must has a unique ID, Supplied by the Android:id attribute.
Note: In order for the Android system to revert to the state of your activity's comments, each view must have a unique ID that provides the Android:id property.

To save additional data on the activity state, you must override the Onsaveinstancestate () callback method. The system calls this method while the user is leaving your activity and passes it the Bundle object that would be saved in The event, your activity is destroyed unexpectedly. If the system must recreate the activity instance later, it passes the same Bundle object to both the Onrestoreinstancesta Te () and OnCreate () methods.
To save additional data about activity status, you must override the Onsaveinstancestate () callback method. The system calls this method when the user leaves your activity and passes for it a bundle object that will be saved in your activity event that was accidentally destroyed by the event. If the system must recreate the activity instance later, it passes the same Bundel bundle object both Onrestoreinstancestate () and the OnCreate () method.

Figure 2. As the system begins to stop your activity, it calls Onsaveinstancestate () (1) So you can specify additional state data yo U ' d like-to-save in case the Activity instance must is recreated. If the activity is destroyed and the same instance must being recreated, the system passes the state data defined at (1) to B Oth the OnCreate () method (2) and the Onrestoreinstancestate () method (3).
Figure 2. When the system starts to stop your activity, it's onsaveinstancestate call (1), so you can specify other country data that you want to save in case the activity instance must be recreated. If the activity is corrupted and must be recreated on the same instance, the system passes the state data (1) in the definition to both the OnCreate () method (2) and the Onrestoreinstancestate () method (3).

Save Your Activity State
Save your activity status


--------------------------------------------------------------------------------
As your activity begins to stop, the system calls Onsaveinstancestate () so your activity can save state information with a Collection of key-value pairs. The default implementation of this method saves information about the state of the activity ' s view hierarchy, such as the Text in an EditText widget or the scroll position of a ListView.
When your activity starts and stops, the system calls the Onsaveinstancestate () so that your activity can save the collection state information with key-value pairs. The default implementation of this method holds information about the activity hierarchy, such as the text in a EditText plugin or the scroll position of the ListView.

To save additional state information for your activity, you must implement Onsaveinstancestate () and add key-value pairs t o the Bundle object. For example:
In order to save additional state information for your activity, you must implement the Onsaveinstancestate () and add the bundle object for the key-value pair. 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);

Superclass so it can save the view hierarchy state
Super.onsaveinstancestate (savedinstancestate);
}

Caution:always Call the superclass implementation of onsaveinstancestate () so the default implementation can save the STA Te of the view hierarchy.
Note: The onsaveinstancestate execution of the parent class is always called (), so the default implementation can save the state of the view hierarchy.

Restore Your Activity State
Recovery activity Status


--------------------------------------------------------------------------------

When your activity is recreated after it's previously destroyed, you can recover your saved state from the Bundle of that T He system passes your activity. Both the OnCreate () and Onrestoreinstancestate () callback methods receive the same Bundle that contains the instance state Information.
When your activity is rebuilt and previously destroyed, you can restore the saved state from the Bundel bundle, the system through your activity. Both the OnCreate () and Onrestoreinstancestate () callback methods receive the same bundle that contains the instance state information.

Because the OnCreate () method is called whether the system is creating a new instance of your activity or recreating a pre Vious One, you must check whether the state Bundle was null before you attempt to read it. If it is null and then the system is creating a new instance of the activity, instead of restoring a previous one that was de Stroyed.
Because the OnCreate () method is called by whether the system is creating a new instance of the activity or re-creating it before, you must check the state before you try to read it Bundel whether the bundle is empty. If it is empty, the system creates a new instance of the activity instead of restoring the previous one that was destroyed.

For example, here's how you can restore some state data in OnCreate ():
For example, here's how to restore some state data in the OnCreate ():

@Override
protected void onCreate (Bundle savedinstancestate) {
     Super.oncreate (savedinstancestate); Always call the superclass first
  
   //Check Whether we ' re recreating a previously Destroyed instance
    if (savedinstancestate! = null) {
        //Restore value of saved state
        Mcurrentscore = Savedin Stancestate.getint (State_score);
        mcurrentlevel = Savedinstancestate.getint (state_level);
   } else {
       //Probably Initialize members with Default values for a new instance
   }
    ...
}

instead of restoring the state during onCreate () a may choose to implement Onrestoreins Tancestate (), which the system calls after the OnStart () method. The system calls Onrestoreinstancestate () only if there are a saved state to restore, so do not need to check whether T The He Bundle is null:
instead of OnCreate () in the recovery state, you can choose to implement Onrestoreinstancestate () after the OnStart () method, which is called by the system. System call Onrestoreinstancestate () only if there is a saved state recovery, so you do not need to check whether the Bundel bundle is empty:

public void Onrestoreinstancestate (Bundle savedinstancestate) {
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);
}

Caution:always Call the superclass implementation of onrestoreinstancestate () so the default implementation can restore T He state of the view hierarchy.
Note: The parent class is always called to implement Onrestoreinstancestate (), so the default implementation can restore the state of the view hierarchy.

To learn more on recreating your activity due to a restart event at runtime (such as while the screen rotates), read Han Dling Runtime changes.
To learn more about recreating activity due to restarting events at run time (when screen rotation, etc.), read the changes in processing runtime.

Next class:building a Dynamic UI with fragments
Next lesson: Creating a dynamic UI and fragment

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

Android official Getting Started document [15] recreate an activity event

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.