您的位置:首页 > 移动开发 > Android开发

Android官网恢复(重新创建)Activity文档翻译:Recreating an Activity

2015-08-17 15:48 656 查看
原文档路径(需翻墙)
http://developer.android.com/index.html
也可以通过你本地的Android SDK进入:E:\android-sdk-windows\docs\training\index.html(建议不要联网,会很慢;火狐浏览器有离线功能:文件>脱机工作)

页面内路径:Develop > Training > Managing the Activity Lifecycle > Recreating an Activity

Recreating an Activity

PreviousNext


This lesson teaches you to

Save
Your Activity State
Restore
Your Activity State


You should also read

Supporting
Different Screens
Handling
Runtime Changes
Activities

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正常运行时(onResume状态),用户点击返回按钮或者在Activity所在的代码中执行其finish()方法都会销毁该Activity。但是如果这个Activity不可见并且长时间没有被使用,或者前台的Activity(当前onResume状态下的Activity)需要更多的资源(或内存)时,此时Android系统必须关闭掉后台进程从而释放内存。
When your activity is destroyed because the user presses Backor 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.
当用户点击了返回按钮或者在代码中执行了finish()方法时,系统会默认永久关闭该Activity,因为当前程序的运行状态显示该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都会被销毁并且重建。当屏幕的方向发生变化时系统会销毁并且重建前台Activity,因为屏幕的布局结构已经变化了,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.
默认情况下,系统会用Bundle对象保存Activity布局中的每一个View对象的信息(例如EditText的文本值等);因此,如果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中的每一个view的状态,你必须通过android:id的方式为每一个view指定一个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但不关闭它时Bundle对象会保存数据,一旦系统要回收该Activity该onSaveInstanceState()方法就会被调用。如果之后系统必须重建该Activity实例,该Bundle对象就会传递到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需要被重建你可以存放一些额外的数据到Bundle对象中。如果Activity被销毁了并且需要重建出原来的Activity,系统会将在(1)中存储的数据传递到onCreate()方法和onRestoreInstanceState()方法中。


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 a
ListView
.
当你的Activity有被系统回收的趋势时,系统会调用onSaveInstanceState()方法,因此,你的Activity可以通过键值对集合的形式保存状态数据。这个方法默认会保存Activity中view的层级状态数据,例如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) {
// 保存用户当前游戏的状态
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

// 总是调用父类的该方法,才能保存Activity的view的层级状态。
super.onSaveInstanceState(savedInstanceState);
}


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

警告:总是调用父类的该方法,才能保存Activity的view的层级状态(非额外的数据)。


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.
当你的Activity被系统回收之后需要重新创建时,你可以从系统传递给你的Activity的Bundle对象中恢复之前保存的数据。其中onCreate()方法和onRestoreInstanceState()方法都会接收到该包含了之前Activity实例状态信息的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对象之前,必选先判断它是否为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

// 判断我们是否是在恢复之前销毁的Activity实例
if (savedInstanceState != null) {
// 从保存的数据中恢复成员变量的值
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
} else {
// 初始化新的Activity成员变量等(非恢复之前的Activity)
}
...
}

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()方法之外,你也可以选择重写在onStart()方法之后执行的onRestoreInstanceState()方法。并且只有当有被保存的数据需要恢复时该方法才会被调用,因此,你不必去验证Bundle对象是否为null:

public void onRestoreInstanceState(Bundle savedInstanceState) {
// 总是调用父类的方法才会恢复view的层级数据
super.onRestoreInstanceState(savedInstanceState);

// 从Bundle对象中恢复成员变量的值
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: