[Android training video series] 2.4 recreating an activity

Source: Internet
Author: User

1. Main Content
This section describes how to use onsaveinstancestate and onrestoreinstancestate.

2. Video description

Http://www.eyeandroid.com/thread-11391-1-1.html

3. Translation Reference
In some cases, your activity will be destroyed due to common program behaviors. For example, when a user clicks the back button or calls the finish () method by himself. In addition, if the activity is stopped and not used for a long time, or the foreground system needs more resources, the system may destroy your activity.

When your activity ends by pressing the back key or by yourself, the system thinks that this activity instance is no longer needed, because your behavior clearly indicates that you want to destroy it. However, if the system forces the destruction of your activity, although the activity instance is no longer available, the system can remember its existence. When the user returns to this program, the system will re-create an activity instance using the state saved when the activity is destroyed. The stored data used by this system to restore the previous state is called "instance state", and some key-Value Sets are stored in the bundle object. Warning when a user rotates the screen, your activity will be destroyed and re-created. Because the screen configuration has changed, your activity may also need to load other selectable resources (such as layout ). By default, the system uses the bundle instance status to save the information of each view object in the layout (for example, the text value in the edittext object ). Therefore, if your activity object is destroyed and re-created, the layout state will be restored to the previous state. In any case, your activity may have a lot of status information to be restored, such as tracking member variables of user processes. To save some extra data to the instance status, an additional lifecycle callback function is provided, which is not shown in the legend of the previous course. This function is called onsaveinstancestate (), which is called when the user leaves the activity. When your activity is accidentally destroyed, the system will call this function, and the bundle object of the function will be saved, so you can add additional information to this bundle. Then, if the system must restart the destroyed activity, it will pass the same bundle object to the onrestoreinstancestate () method and oncreate () method of the activity. Illustration: when the system starts to stop your activity, it calls the onsaveinstancestate () (1) method, so you can save the specified additional status data. If the activity is destroyed and the same instance is re-created, the system passes the state defined in (1) To oncreate () (2) and onrestoreinsatancestate () (3 ). Save your activity status When your activity stops, the system calls onsaveinstancestate (). You can use the set of key-value pairs in this function to save information. The default function saves the view status of the activity, such as the edittext text or the position of the listview scroll bar. To save the additional information, you must implement the onsaveinstancestate () method and add the key-value pair to the bundle object. For example:
  1. Static final string state_score = "playerscore ";
  2. Static final string state_level = "playerlevel ";
  3. ...
  4. @ Override
  5. Public void onsaveinstancestate (bundle savedinstancestate ){
  6. // Save the current game status of the user
  7. Savedinstancestate. putint (state_score, mcurrentscore );
  8. Savedinstancestate. putint (state_level, mcurrentlevel );
  9. // Normally, the parent class is called to save view information.
  10. Super. onsaveinstancestate (savedinstancestate );
  11. }

Warning generally, you need to call its parent class method in the onsaveinstancestate () implementation to save view information. Restore your activity status

If your activity is restarted after it is destroyed, you can restore the stored status from the bundle passed to you by the system, oncreate () and onrestoreinstancestate () can receive the same bundle containing instance status information. The oncreate () function is called when the system creates a new activity instance or re-creates the previous activity. Therefore, you must check whether it is empty before reading the bundle, if it is empty, the system creates a new instance. If not, the previous destroyed activity is restored. For example, here is how to restore data in the oncreate () method:
  1. @ Override
  2. Protected void oncreate (bundle savedinstancestate ){
  3. Super. oncreate (savedinstancestate); // always call the superclass first
  4. // Check whether an activity instance has been destroyed.
  5. If (savedinstancestate! = NULL ){
  6. // Restore the player status
  7. Mcurrentscore = savedinstancestate. getint (state_score );
  8. Mcurrentlevel = savedinstancestate. getint (state_level );
  9. } Else {
  10. // Initialize a default value for the new instance
  11. }
  12. ...
  13. }

You can also choose to implement it in the onrestoreinstrancestate () method. After the system calls the onstart () function, it will call onrestoreinstrancestate (), but it will only be called when the State needs to be restored, therefore, you do not need to check whether the bundle is empty:

  1. Public void onrestoreinstancestate (bundle savedinstancestate ){
  2. // Call the parent class to restore the default view status
  3. Super. onrestoreinstancestate (savedinstancestate );
  4. // Restore the player status
  5. Mcurrentscore = savedinstancestate. getint (state_score );
  6. Mcurrentlevel = savedinstancestate. getint (state_level );
  7. }

Warning generally, you need to call the onrestoreinstancestate () parent class method to restore the default view State. To learn more about rebuilding an activity (such as screen rotation) during running, see handling runtime changes.

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.