为了弄清楚onSaveInstanceState()方法和onRestoreInstanceState()方法,我翻译一下谷歌的API,翻译如下:

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 是由于正常的应用程序行为销毁的,如用户按下返回按钮或调用finish()销毁的。但有的时候也可能是因为当前的activity处于stop状态并且长时间没有被用到或者前台活动需要更多的资源而被系统关闭来获得内存空间。

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 Bundleobject.

当activity是由于用户按back键或者自己finish掉而销毁的话,系统就会认为那个销毁的activity实例会永远消失,因为这个行为表明了这个activity永远都不需要了。然而,如果是因为系统强制销毁掉的话(非正常形式的销毁),那么尽管实际的activity实例已经不存在了,但是系统仍会记住这个activity实例的存在,如果用户再次返回到这个activity的时候,系统会创建一个新的activity实例用来保存这个activity被销毁时候的那个状态信息。被保存的这个数据,也就是系统用来恢复先前状态的那个数据被称为“实例状态”,以key-value的形式存储在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可能需要加载其他资源,如下图:

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).

图2:当系统开始stop你的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 aListView.

当activity开始stop的时候,系统会调用onSaveInstanceState() 方法,所以你的activity可以在这个方法里用一个集合以key-value 的形式保存状态信息。此方法默认保存一些关于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()方法并且添加key-value到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);
}


Restore Your Activity State(恢复Activity状态)

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曾经被销毁后又被重新创建,你就可以通过这个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.

无论是系统创建一个新的activity实例还是再次创建曾经的activity实例,onCreate()方法都会被调用,由于这个原因,你必须在你接受这个Bundle之前检查这个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
// 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()方法,所以你就不需要检查Bundle是否为null了。

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);
}


Activity的onSaveInstanceState()和onRestoreInstanceState()以及API详解的更多相关文章

  1. Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法:

    Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法: 1. 基本作用: Activity的 onSaveInstan ...

  2. Activity的onSaveInstanceState()和onRestoreInstanceState()方法

    首先Android的Activity生命周期如下图: Activity的onSaveInstanceState()和onRestoreInstanceState()并不是生命周期方法,他们不同于onC ...

  3. Android Developer -- Bluetooth篇 开发实例之四 API详解

    http://www.open-open.com/lib/view/open1390879771695.html 这篇文章将会详细解析BluetoothAdapter的详细api, 包括隐藏方法, 每 ...

  4. Java 8 Stream API详解--转

    原文地址:http://blog.csdn.net/chszs/article/details/47038607 Java 8 Stream API详解 一.Stream API介绍 Java8引入了 ...

  5. jqGrid APi 详解

    jqGrid APi 详解 jqGrid皮肤 从3.5版本开始,jqGrid完全支持jquery UI的theme.我们可以从http://jqueryui.com/themeroller/下载我们所 ...

  6. hibernate学习(2)——api详解对象

    1   Configuration 配置对象 /详解Configuration对象 public class Configuration_test { @Test //Configuration 用户 ...

  7. 网络编程socket基本API详解(转)

    网络编程socket基本API详解   socket socket是在应用层和传输层之间的一个抽象层,它把TCP/IP层复杂的操作抽象为几个简单的接口供应用层调用已实现进程在网络中通信. socket ...

  8. 转】Mahout推荐算法API详解

    原博文出自于: http://blog.fens.me/mahout-recommendation-api/ 感谢! Posted: Oct 21, 2013 Tags: itemCFknnMahou ...

  9. dom4j api 详解--XPath 节点详解

    dom4j api 详解 http://871421448.iteye.com/blog/1546955 XPath 节点 http://www.w3school.com.cn/xpath/xpath ...

随机推荐

  1. BZOJ 2329: [HNOI2011]括号修复( splay )

    把括号序列后一定是))))((((这种形式的..所以维护一个最大前缀和l, 最大后缀和r就可以了..答案就是(l+1)/2+(r+1)/2...用splay维护,O(NlogN). 其实还是挺好写的, ...

  2. parquet code demo

    http://www.programcreek.com/java-api-examples/index.php?source_dir=hiped2-master/src/main/java/hip/c ...

  3. 关于LZO和LZOP

    LZO  是一个适合实时解压.压缩的压缩库 LZOP 基于LZO库的压缩解压工具   PS:有了压缩解压库LZO,还不能直接操作文件压缩解压,需要LZOP   下载的话直接google吧~~~  

  4. createNewFile创建空文件夹与createTempFile创建临时文件夹

    创建要注意的地方如下: <pre name="code" class="java"> File类的createNewFile根据抽象路径创建一个新的 ...

  5. 网页往数据库里插数据要用utf8,否则就乱码

    把网页的这行<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ...

  6. linux-0.11抠代码-GDB+VMWARE

    vmware新建一个虚拟机,硬盘为0.1G,建立完成后要先启动一次虚拟机,此时无任何系统,然后再关闭,应该会多出一个ostest-flat.vmdk这个虚拟磁盘文件,下面要用到 新建完成后 我的虚拟机 ...

  7. Servlet、SPringMVC、Struts等防止表单反复提交的多种处理方法

    第一种处理方法(非拦截器): 眼下这样的方法不建议,由于JSP规范不建议写JAVA代码.这样的能够方便另外一种处理方法的理解,另外一种方法引入拦截器的思想,原理基本一样,模仿Struts的Token机 ...

  8. adb shell 命令

    adb 概述 SDK的Tools文件夹下包含着Android模拟器操作的重要命令adb,adb的全称为(Android Debug Bridge就是调试桥的作用.通过adb我们可以在Eclipse中方 ...

  9. PHP程序猿必须学习的第二课——站点安全问题预防

    作为PHP程序猿.第一课我们学习了基本的语法.那么在熟悉基本的语法之后我们应该学些什么呢?我觉得是安全问题.安全问题基于一个站点宛如基石,一着不慎,意味着灾难性的事故. 这里主要就提三点最简单,也是最 ...

  10. PHP 学习1- 函数之error_reporting(E_ALL ^ E_NOTICE)详细说明

    在4.3.0中运行正常,在4.3.1中运行会提示Notice:Undefined varialbe:tmp_i 问题下下: 1.问题出在哪里? 2.应如何修改这段代码? 3.不改段代码,如何修改php ...