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

简介: Android官方入门文档[15]重新创建一个Activity活动Recreating an Activity重新创建一个Activity活动 This lesson teaches you to1.

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

目录
相关文章
|
12月前
|
XML 数据库 Android开发
探索Android开发:从入门到精通的旅程
在这篇文章中,我们将一起踏上一段激动人心的旅程,通过深入浅出的方式,解锁Android开发的秘密。无论你是编程新手还是有经验的开发者,本文都将为你提供宝贵的知识和技能,帮助你构建出色的Android应用。我们将从基础概念开始,逐步深入到高级技巧和最佳实践,最终实现从初学者到专家的转变。让我们开始吧!
271 3
|
Android开发
Android面试之Activity启动流程简述
Android面试之Activity启动流程简述
242 6
|
消息中间件 Android开发 索引
Android面试高频知识点(4) 详解Activity的启动流程
Android面试高频知识点(4) 详解Activity的启动流程
241 3
|
缓存 前端开发 Android开发
Android实战之如何截取Activity或者Fragment的内容?
本文首发于公众号“AntDream”,介绍了如何在Android中截取Activity或Fragment的屏幕内容并保存为图片。包括截取整个Activity、特定控件或区域的方法,以及处理包含RecyclerView的复杂情况。
221 3
|
存储 前端开发 测试技术
Android kotlin MVVM 架构简单示例入门
Android kotlin MVVM 架构简单示例入门
251 1
|
XML Android开发 数据格式
android中两个Activity同时设定了intent-filter的category为android.intent.category.LAUNCHER,会发生什么情况?
本文通过案例分析了在Android中当两个Activity都设置了`android.intent.category.LAUNCHER`类别时,会导致它们同时在应用启动器的"所有应用"页面显示为不同的启动入口。
407 2
android中两个Activity同时设定了intent-filter的category为android.intent.category.LAUNCHER,会发生什么情况?
|
开发框架 移动开发 Android开发
安卓与iOS开发中的跨平台解决方案:Flutter入门
【9月更文挑战第30天】在移动应用开发的广阔舞台上,安卓和iOS两大操作系统各自占据半壁江山。开发者们常常面临着选择:是专注于单一平台深耕细作,还是寻找一种能够横跨两大系统的开发方案?Flutter,作为一种新兴的跨平台UI工具包,正以其现代、响应式的特点赢得开发者的青睐。本文将带你一探究竟,从Flutter的基础概念到实战应用,深入浅出地介绍这一技术的魅力所在。
299 7
|
Android开发 开发者
安卓开发中的自定义视图:从入门到精通
【9月更文挑战第19天】在安卓开发的广阔天地中,自定义视图是一块充满魔力的土地。它不仅仅是代码的堆砌,更是艺术与科技的完美结合。通过掌握自定义视图,开发者能够打破常规,创造出独一无二的用户界面。本文将带你走进自定义视图的世界,从基础概念到实战应用,一步步展示如何用代码绘出心中的蓝图。无论你是初学者还是有经验的开发者,这篇文章都将为你打开一扇通往创意和效率的大门。让我们一起探索自定义视图的秘密,将你的应用打造成一件艺术品吧!
175 10
|
消息中间件 Android开发 索引
Android面试高频知识点(4) 详解Activity的启动流程
讲解Activity的启动流程了,Activity的启动流程相对复杂一下,涉及到了Activity中的生命周期方法,涉及到了Android体系的CS模式,涉及到了Android中进程通讯Binder机制等等, 首先介绍一下Activity,这里引用一下Android guide中对Activity的介绍:
295 4
|
IDE Java 程序员
安卓应用开发入门:打造你的第一个“Hello World”
【9月更文挑战第11天】在编程的世界里,每一个初学者的旅程都从一个简单的“Hello World”开始。本文将带领安卓开发的新手们,通过简单直观的方式,一步步构建出自己的第一个安卓应用。我们将探索安卓工作室(Android Studio)的安装、项目的创建,以及如何运行和调试你的应用。无论你是编程新手还是想扩展技能的老手,这篇文章都将为你打开一扇通往安卓世界的大门。
422 8