Activity的onSaveInstanceState()和onRestoreInstanceState()以及API详解

       为了弄清楚onSaveInstanceState()方法和onRestoreInstanceState()方法,我翻译一下谷歌的API,翻译如下:

       There are a few scenarios in which your activity is 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.

       在一些情况下,你的activity 是由于正常的应用程序行为销毁的,如用户按下返回按钮或调用finish()销毁的。但有的时候也可能是因为当前的activity处于stop状态并且长时间没有被用到或者前台活动需要更多的资源而被系统关闭来获得内存空间。

      When your activity is destroyed because the user presses Back or the activity finishes itself, the system's concept of that 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), then although the actual Activity instance is gone, the system remembers that it existed such that if the user navigates back to it, the system creates a new instance of the activity using a set of saved data that describes the state of the activity when it was destroyed. The saved data that the system uses to restore the previous state is called the "instance state" and is a collection of key-value pairs stored in a Bundleobject.

      当activity是由于用户按back键或者自己finish掉而销毁的话,系统就会认为那个销毁的activity实例会永远消失,因为这个行为表明了这个activity永远都不需要了。然而,如果是因为系统强制销毁掉的话(非正常形式的销毁),那么尽管实际的activity实例已经不存在了,但是系统仍会记住这个activity实例的存在,如果用户再次返回到这个activity的时候,系统会创建一个新的activity实例用来保存这个activity被销毁时候的那个状态信息。被保存的这个数据,也就是系统用来恢复先前状态的那个数据被称为“实例状态”,以key-value的形式存储在Bundle对象中。

      Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).

      注意:每次用户旋转屏幕的时候activity将会被销毁并再次创建。当屏幕方向变化时,系统会销毁并且再次创建前台活动,因为屏幕配置已经改变了并且你的activity可能需要加载其他资源,如下图:

      

      Figure 2. As the system begins to stop your activity, it calls onSaveInstanceState() (1) so you can specify additional state data you'd like to save in case the Activity instance must be recreated. If the activity is destroyed and the same instance must be recreated, the system passes the state data defined at (1) to both the onCreate() method (2) and theonRestoreInstanceState() method (3).

      图2:当系统开始stop你的activity的时候,就会调用 onSaveInstanceState()(1)所以你能指定那些你想要保存下来的那些额外的状态数据来让activity实例再现,如果activity被销毁了并且必须要创建一个相同的实例,系统会通过(1)中被定义的状态数据在onCreate()(2)方法和onRestoreInstanceState()(3)中使用。 

      我打印了一下生命周期,大家可以看一下: 

      

Save Your Activity State(保存Activity状态)

       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 aListView.

      当activity开始stop的时候,系统会调用onSaveInstanceState() 方法,所以你的activity可以在这个方法里用一个集合以key-value 的形式保存状态信息。此方法默认保存一些关于activity视图的信息,如EditText的文本信息或者ListView的滚动位置。

      To save additional state information for your activity, you must implement onSaveInstanceState() and add key-value pairs to the Bundle object. For example:

      为了给你的activity保存额外的状态信息,你必须要实现onSaveInstanceState()方法并且添加key-value到Bundle对象,例如:    

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);
}

     

Restore Your Activity State(恢复Activity状态)

       When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system passes your activity. Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information.

       当activity曾经被销毁后又被重新创建,你就可以通过这个Bundle来找回你保存的状态 ,在 onCreate()方法里和onRestoreInstanceState()方法里来接受包含实例状态信息的Bundle都可以。

       Because the onCreate() method is called whether the system is creating a new instance of your activity or recreating a previous one, you must check whether the state Bundle is null before you attempt to read it. If it is null, then the system is creating a new instance of the activity, instead of restoring a previous one that was destroyed.

      无论是系统创建一个新的activity实例还是再次创建曾经的activity实例,onCreate()方法都会被调用,由于这个原因,你必须在你接受这个Bundle之前检查这个Bundle是否为null,如果是null,那么系统会创建一个新的activity实例而不是恢复先前被销毁的那个activity实例。

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

       例如,下面就是如何在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 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  可能初始化一个新的实例的一些默认值
    }
}

     Instead of restoring the state during onCreate() you may choose to implement onRestoreInstanceState(), which the system calls after the onStart() method. The system calls onRestoreInstanceState() only if there is a saved state to restore, so you do not need to check whether the Bundle is null:  

       也可以不在onCreate()期间恢复状态,你可以选择去实现onRestoreInstanceState()方法,在系统调用onStart()方法的时候。只有在有一个保存的状态需要恢复的时候,系统才会去调用onRestoreInstanceState()方法,所以你就不需要检查Bundle是否为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);
}

       

      

      

      

      

原文地址:https://www.cnblogs.com/chentong/p/5482201.html