Android官方入门文档[15]重新创建一个Activity活动
Android官方入门文档[15]重新创建一个Activity活动
Recreating an Activity
重新创建一个Activity活动
This lesson teaches you to
1.Save Your Activity State
2.Restore Your Activity State
You should also read
?Supporting Different Screens
?Handling Runtime Changes
?Activities
这节课教你
1.保存您的Activity活动状态
2.恢复您的Activity活动状态
你也应该阅读
?支持不同的屏幕
?处理运行时的变化
?Activity活动
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活动由于正常的应用程序的行为破坏,当用户按下返回按钮或activity通过调用光洁度信号自身的破坏()如。该系统还可以摧毁你的activity,如果它当前已停止,并在很长一段时间没有被使用或前台Activity活动需要更多的资源,因此系统必须关闭后台进程以恢复内存。
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 Bundle object.
因为当用户按下返回或Activity活动完成本身的activity被破坏,该Activity活动实例的系统的概念已经一去不复返了,因为行为表示activity不再需要。然而,如果系统破坏该Activity活动由于系统限制(而不是正常的应用程序的行为),则虽然实际的Activity活动实例被消失了,系统记住它存在使得如果用户导航回到它,系统创建一个新的例如使用一组保存的数据的描述,当它被破坏该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).
注意:您的activity将被销毁,每个用户旋转屏幕时重建。当屏幕改变方向时,系统会破坏并重新创建前景活性,因为在屏幕的配置已经改变和您的活性可能需要加载替代资源(如布局)。
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.
默认情况下,系统采用Bundel捆绑实例状态保存每个视图对象的信息在你的Activity活动布局(如进入一个EditText对象的文本值)。所以,如果你的Activity活动实例被破坏,重建,布局的状态恢复到以前的状态,没有按你所需要的代码。然而,你的Activity活动可能有你想恢复,如跟踪Activity活动的用户的进度成员变量更多的状态信息。
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.
注:为了使Android系统恢复在您的Activity活动的意见的状态,每个视图必须具有唯一的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.
要保存有关Activity活动状态附加数据,您必须覆盖的onSaveInstanceState()回调方法。该系统调用这个方法,当用户离开你的Activity活动,并为其传递一个将被保存在您的Activity活动被意外破坏了该事件的Bundle对象。如果系统必须稍后重新创建Activity活动实例,它通过相同的Bundel捆绑对象既onRestoreInstanceState()和的onCreate()方法。
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 the onRestoreInstanceState() method (3).
图2.当系统开始停止你的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 a ListView.
当你的Activity活动开始,停止,系统调用的onSaveInstanceState(),这样你的Activity活动可以保存用键 - 值对的集合状态信息。此方法的默认实现保存有关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(),并添加键值对的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 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.
当你的活动被重建后,以前被破坏,可以从Bundel捆绑恢复保存的状态,该系统通过你的活动。双方的onCreate()和onRestoreInstanceState()回调方法收到包含实例状态信息的同捆。
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()方法被调用的系统是否是创建活动的新实例或重新创建以前,你必须检查你尝试读取它之前的状态Bundel捆绑是否为空。如果为空,则系统创建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()只如果有保存的状态恢复,所以你不需要检查Bundel捆绑是否为空:
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.
要了解更多关于重新创建activity由于重启的事件在运行时(当屏幕旋转等),请阅读处理运行时的变化。
Next class: Building a Dynamic UI with Fragments
下一节课:建立一个动态UI与片段
本文翻译自:https://developer.android.com/training/basics/activity-lifecycle/recreating.html
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。