码迷,mamicode.com
首页 > 其他好文 > 详细

Recreating an Activity 重新创建一个活动

时间:2014-06-01 18:18:52      阅读:550      评论:0      收藏:0      [点我收藏+]

标签:des   android   c   style   class   blog   

There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses theBack 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.

有几个场景中你的活动破坏是由于正常的应用程序行为,如当用户按下返回按钮或着通过调用finish()方法发出你毁灭自身活动的信号。 系统也会摧毁你的活动直到目前停止,不会用很长一段时间,也许前台活动需要更多的资源,系统必须关闭后台进程恢复记忆。

When your activity is destroyed because the user presses Backor the activity finishes itself, the system‘s concept of thatActivity 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 Bundle object.

当你的活动被摧毁,因为用户按下返回按钮而使得活动自身结束。这个Activity实例本身系统的概念是一去不复返,因为这个行为表示活动不再需要。 然而,如果系统破坏 活动由于系统约束(而不是正常的应用程序的行为),那么尽管实际Activity 实例已经不在,系统记得它的存在,这样如果用户导航回它,系统将创建一个新的实例使用一组保存的数据来描述活动的状态时被毁。 保存的数据表明系统使用恢复以前的状态称为“实例状态”,它们是一组存储在一个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).

注意:用户每次旋转屏幕时您的活动将会被销毁然后重新创建。当屏幕方向改变,系统销毁和再现前台的活动,因为屏幕的设置已经改变了,而且您的活动需要加载可替代的资源(例如布局)。

By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText object). So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you. However, your activity might have more state information that you‘d like to restore, such as member variables that track the user‘s progress in the activity.

默认的情况下,系统使用Bundle实例来保存在您的活动布局中的每一个View对象(例如键入到EditText对象中的文本值)。因此,如果您的活动实例被销毁并且重新创建,布局状态会还原到以前的状态,而没有您所需的代码。然而,您的活动可能有您想要恢复的更多的状态信息,例如跟踪活动中用户进展的成员变量。

Note: In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id attribute.

注意:为了在您的活动使得andriod系统来恢复视图状态,每一个视图必须有一个独一无二的ID,即android所提供的ID属性:android:id  。

To save additional data about the activity state, you must override the onSaveInstanceState() callback method. The system calls this method when the user is leaving your activity and passes it the Bundle object that will be saved in the event that your activity is destroyed unexpectedly. If the system must recreate the activity instance later, it passes the same Bundle object to both the onRestoreInstanceState() and onCreate() methods.

为了节省格外的活动状态的数据,您必须重写onSaveInstanceState()回调方法。当用户离开您的活动,通过它的 Bundle 对象保存在您的活动被意外销毁的状态时,系统会调用这个方法。如果系统必须重新创建活动实例,它将通过相同的 Bundle对象来传递给onRestoreInstanceState() onCreate() 方法

bubuko.com,布布扣

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

图二所示,当系统开始停止您的活动,它回调onSaveInstanceState()(1)方法,您可以指定额外的您想要保存的状态数据,以防 Activity实例要被重新创建。如果活动被销毁,相同的实例必须被荣新创建,系统通过在(1)中的状态数据来传递给 onCreate()方法(2)和onRestoreInstanceState()方法 (3).

Save Your Activity State

保存您的活动状态


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.

当您的活动开始停止,您的系统调用onSaveInstanceState()方法,因此您的活动可以通过使用一组键值对来保存状态信息。这个方法默认保存有关于活动的视图层次的信息,例如在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:

为了替您的活动节省格外的状态信息,您必须执行onSaveInstanceState()方法,并且添加键值对到 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);
}

Caution: Always call the superclass implementation of onSaveInstanceState() so the default implementation can save the state of the view hierarchy.

注意:总是调用onSaveInstanceState()方法的父类实现,所以默认的实现可能保存试图层次的状态。

Restore Your Activity State

恢复您的活动状态


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

当您的活动在以前被销毁之后重新创建,您可以通过系统传递到您的活动中的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.

由于onCreate() 方法被调用,无论系统是否是创建一个新的活动实例还是重新创建一个以前的,您必须在您阅读之前确定状态Bundle 对象是否为空,然后系统创建了一个新的活动实例而不是恢复已经被销毁的那个以前的实例。

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()方法,onRestoreInstanceState()方法是系统在调用 onStart()方法之后调用的。系统调用 onRestoreInstanceState()方法只有一个保存状态恢复,因此您不必检查Bundle对象是否为空:

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

Caution: Always call the superclass implementation of onRestoreInstanceState() so the default implementation can restore the state of the view hierarchy.

注意:总是调用onRestoreInstanceState()实现方法的父类,因此默认的实现可以恢复视图层次的状态。

To learn more about recreating your activity due to a restart event at runtime (such as when the screen rotates), read Handling Runtime Changes.

为了学习更多的有关于由于在运行时重启的活动而重新创建您的活动(例如当屏幕旋转),阅读Handling Runtime Changes.

Recreating an Activity 重新创建一个活动,布布扣,bubuko.com

Recreating an Activity 重新创建一个活动

标签:des   android   c   style   class   blog   

原文地址:http://blog.csdn.net/xia09222826/article/details/27964209

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!