Recreating an 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 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() 方法

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

时间: 2024-10-13 06:57:48

Recreating an Activity 重新创建一个活动的相关文章

Android官方入门文档[15]重新创建一个Activity活动

Android官方入门文档[15]重新创建一个Activity活动 Recreating an Activity重新创建一个Activity活动 This lesson teaches you to1.Save Your Activity State2.Restore Your Activity State You should also read?Supporting Different Screens?Handling Runtime Changes?Activities 这节课教你1.保存

Android学习笔记(二)——探究一个活动

//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 活动(Activity)是最容易吸引到用户的地方了,它是一种可以包含用户界面的组件,主要用于和用户进行交互.一个应用程序中可以包含零个或多个活动.活动是Android四大组件之一,下面我们来探究一个活动. 1.创建一个活动: 我们可以按照学习笔记(一)中的流程创建了一个活动,创建之后可以看见IDE已经为我们写好的默认onCreate()方法: 默认onCreate()方法非常简单,就是调用了父类的 onCreate(

Starting an Activity 开启一个活动

Starting an Activity 开启一个活动 Previous NextGet  started This lesson teaches you to 这个课程教你去了解以下: Understand the Lifecycle Callbacks       理解生命周期回调 Specify Your App's Launcher Activity     指定应用程序的启动活动 Create a New Instance                           创建一个新

Android 创建一个新的Activity

本文转载自:http://www.cnblogs.com/wuyudong/p/5658020.html 新建一个项目,创建一个java类,继承自Activity 新建一个Android.xml文件:activity_test.xml 这里需要在清单文件里面配置一下 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.

创建一个activity

在了解Android项目的文件结构后,接下来的是创建一个Activity.一个Android程序是很多个Activity组成的,它是android程序一个非常重要的组成部分之一,如何创建一个Activity?下面简单介绍如何创建一个Activity. 一,创建一个继承自Activity的Java类; package com.androidwhy; import android.app.Activity; import android.os.Bundle; public class HelloAc

创建一个新的Activity

1.创建一个类继承Activity类,并创建对应的布局文件,在onCreate方法中加载该布局. 2.在AndroidManifest.xml声明该组件 注:如果想配置一个activity在桌面上有该activity的启动图标需要在AndroidManifest.xml中配置该activity属性为 <intent-filter> <action android:name="android.intent.action.MAIN" /> <category

01_创建一个新的activity&amp;activity配置清单文件

今天开始学四大组件.今天是学Activity,然后是广播接收者,然后是服务,然后是内容提供者.四大组件,咱们一天一个.Activity就是跟用户交互的界面,大部分的应用都不会只有这么一个界面.创建多个Activity,在不同的页面之间进行跳转并且在不同的页面之间进行数据的传递. 创建一个新的Activity,想办法把它给打开.打开一个Activity就涉及到一个隐式意图和一个显式意图.intent,第二天的时候打电话,电话拨号器就用到了这个东西. Activity的生命周期.讲Servlet的时

创建一个新的安卓应用程序 设置主Activity

File--New--Android application Project 主类继承Activity public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.lay

Recreating an Activity

有几个场景中你的activity会销毁是由于正常的app行为,例如当用户按下后退按钮或通过调用finish()方法销毁自身.系统也会销毁你的activity如果是目前停止并长时间没有被使用或前台activity需要更多的资源,所以系统必须关闭后台进程去回收内存. 当你的activity是因为用户按下返回键而被摧毁时,activity实例在系统中是被永久销毁了,因为该行为表示activity不再是被需要的了.然而,如果系统销毁activity时由于系统约束(而不是正常程序行为),虽然实际的acti