为了弄清楚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. 表单验证插件 jquery.validata 使用方法

    参考资料:http://www.runoob.com/jquery/jquery-plugin-validate.html 下载地址 jquery.validate插件的文档地址http://docs ...

  2. Spring—Hibernate

    1.家jar包 2配置applicationContext与xxx.hbm.xml(根据需要决定是否配置hibernate.hbm.xml) applicationContext.xml <?x ...

  3. cocos2d-x3.9利用cocos引擎一键打包Android平台APK(C++小白教程)

    链接地址:http://www.cocoachina.com/bbs/read.php?tid=333937 cocos2d-x3.9利用cocos引擎一键打包Android平台APK(C++小白教程 ...

  4. BZOJ 1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富( dp )

    dp , dp[ i ][ j ] = max( dp[ k ][ j - 1 ] ) + G[ i ][ j ] ( i - 1 <= k <= i + 1 , dp[ k ][ j - ...

  5. Mysql 库、表、字段 字符集

    show character set;show create database aloe;show create table book_category;show full columns from ...

  6. 【转】System.Data.OracleClient requires Oracle client software version 8.1.7 or greater

    安装完ASP.NET,Oracle9i客户端后,使用System.Data.OracleClient访问Oracle数据库如果出现这种错误:System.Data.OracleClient requi ...

  7. php如何开启GD库

    GD库是干什么用的呢!它是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片.GD库在php中默认是没有开启的,如果想让它支持图片处理功能,那么就要手 ...

  8. sqlserver 只有函数和扩展存储过程才能从函数内部执行

    一个SQLServer的自定义函数中调用一个自定义的存储过程,执行此函数后发出如下提示:“只有函数和扩展存储过程才能从函数内部执行". 原因:函数只能使用简单的sql语句,逻辑控制语句,复杂 ...

  9. java 简单的词法分析

    package com.seakt.example; import java.io.*; import java.lang.String; public class J_Scanner { publi ...

  10. webscraping documentation — webscraping documentation

    webscraping documentation - webscraping documentation webscraping documentation¶ Introduction Backgr ...