Recreating an Activity 重新创建一个Activity
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 callingfinish(). 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 Back or 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 actualActivity 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 aBundle object.
当你的活动被摧毁,因为用户按下或活动完成本身,但是系统的概念实例一去不复返,因为行为表示活动不再是必要的。然而,如果系统由于系统约束(而不是正常程序行为)破坏活动,系统记得Activity实例的存在,这样如果用户导航它,活动的系统创建一个新的实例使用一组保存的数据描述活动的状态时被毁。系统使用保存的数据恢复以前的状态称为“实例状态”,是一组存储在包对象的键值对。
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 eachView object in your activity layout (such as the text value entered into anEditText 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 theandroid:id attribute.
注意:为了在你的活动中让Android系统恢复视图的状态,每一个视图都必须有一个惟一的ID,Android提供的:ID属性。
To save additional data about the activity state, you must override theonSaveInstanceState() callback method. The system calls this method when the user is leaving your activity and passes it theBundle 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 sameBundle object to both theonRestoreInstanceState() andonCreate() methods.
节省额外的活动状态的数据,您必须覆盖仅有onSaveInstanceState()回调方法。系统调用此方法,当用户离开你的活动,通过它的Bundle对象将被保存在意外事件,你的活动被摧毁。如果系统必须重新创建活动实例之后,它将相同的Bundle对象传递给onRestoreInstanceState()和onCreate()方法。
As the system begins to stop your activity, it callsonSaveInstanceState() (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 theonCreate() method (2) and theonRestoreInstanceState() method (3).
随着系统开始停止你的活动,onSaveInstanceState()(1),您可以指定额外的您想保存的状态数据,以防活动实例必须被重新创建。如果活动被摧毁,必须重新创建相同的实例,该系统通过状态数据定义在onCreate()方法(1),(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 anEditText widget or the scroll position of aListView.
作为你的活动开始停止,系统调用仅有onSaveInstanceState(),所以你的活动可以节省状态信息与键值对的集合。此方法的默认实现保存信息活动的状态的视图层次,如文本在EditText部件或滚动ListView的位置。
To save additional state information for your activity, you must implementonSaveInstanceState() and add key-value pairs to theBundle 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 ofonSaveInstanceState() so the default implementation can save the state of the view hierarchy.
警告:总是调用超类实现onSaveInstanceState(),所以默认实现可以保存视图层次的状态。
Restore Your Activity State 恢复你的activity状态
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 theonCreate() andonRestoreInstanceState() callback methods receive the sameBundle 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 stateBundle 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()方法被调用,您必须在你尝试之前阅读它检查状态包是否是空的。如果为空,则系统活动的创建一个新的实例,而不是恢复之前被摧毁。
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 implementonRestoreInstanceState(), which the system calls after theonStart() method. The system callsonRestoreInstanceState() only if there is a saved state to restore, so you do not need to check whether theBundle is null:
而不是在onCreate()恢复状态,你可以选择实现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 ofonRestoreInstanceState() so the default implementation can restore the state of the view hierarchy.
To learn more about recreating your activity due to a restart event at runtime (such as when the screen rotates), readHandling Runtime Changes.
警告:总是调用超类实现onRestoreInstanceState(),所以默认实现可以恢复视图层次的状态。了解更多关于重建你的活动将在运行时重新启动事件时(如屏幕旋转),读rHandling Runtime Changes。