Activity的onSaveInstanceState()和onRestoreInstanceState()以及API详解
为了弄清楚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 Bundle
object.
当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详解的更多相关文章
- Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法:
Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法: 1. 基本作用: Activity的 onSaveInstan ...
- Activity的onSaveInstanceState()和onRestoreInstanceState()方法
首先Android的Activity生命周期如下图: Activity的onSaveInstanceState()和onRestoreInstanceState()并不是生命周期方法,他们不同于onC ...
- Android Developer -- Bluetooth篇 开发实例之四 API详解
http://www.open-open.com/lib/view/open1390879771695.html 这篇文章将会详细解析BluetoothAdapter的详细api, 包括隐藏方法, 每 ...
- Java 8 Stream API详解--转
原文地址:http://blog.csdn.net/chszs/article/details/47038607 Java 8 Stream API详解 一.Stream API介绍 Java8引入了 ...
- jqGrid APi 详解
jqGrid APi 详解 jqGrid皮肤 从3.5版本开始,jqGrid完全支持jquery UI的theme.我们可以从http://jqueryui.com/themeroller/下载我们所 ...
- hibernate学习(2)——api详解对象
1 Configuration 配置对象 /详解Configuration对象 public class Configuration_test { @Test //Configuration 用户 ...
- 网络编程socket基本API详解(转)
网络编程socket基本API详解 socket socket是在应用层和传输层之间的一个抽象层,它把TCP/IP层复杂的操作抽象为几个简单的接口供应用层调用已实现进程在网络中通信. socket ...
- 转】Mahout推荐算法API详解
原博文出自于: http://blog.fens.me/mahout-recommendation-api/ 感谢! Posted: Oct 21, 2013 Tags: itemCFknnMahou ...
- dom4j api 详解--XPath 节点详解
dom4j api 详解 http://871421448.iteye.com/blog/1546955 XPath 节点 http://www.w3school.com.cn/xpath/xpath ...
随机推荐
- ExtJs007最常用的查询方法
Ext.onReady(function () { Ext.create('Ext.panel.Panel', { title: '我的面板', width: '100%', height: 400, ...
- servlet操作数据库
工具:myeclipse 数据库工具:mysql java ee操作数据库,首先要导入数据库驱动文件,我用的是mysql 刚开始,很多人代码正确但是就是连接不上,原因就是忘了驱动文件的导入. 我的驱动 ...
- codeforces 508D . Tanya and Password 欧拉通路
题目链接 给你n个长度为3的子串, 这些子串是由一个长度为n+2的串分割得来的, 求原串, 如果给出的不合法, 输出-1. 一个欧拉通路的题, 将子串的前两个字符和后两个字符看成一个点, 比如acb, ...
- 转:在控制台中调试AngularJS应用
在控制台中调试AngularJS应用 在创建AngularJS应用时,一个很棘手的问题是如何在Chrome,Firefox,以及IE的JavaScript控制台中访问深藏在应用中的数据和服务.本文将会 ...
- ElasticSearch 插件配置
http://blog.sina.com.cn/s/blog_8f31e5b10101dsnq.html http://www.tuicool.com/articles/mMZfu2 http://b ...
- github 的分支操作
首先需要当前目录设置为仓库目录 一.创建本地分支 1.查看有哪些分支:git branch 2.创建一个分支:git branch name ,其中name是分支名 3.切换到分支:git chec ...
- Windows Azure 网站自愈
编辑人员注释:本文章由 Windows Azure 网站团队的项目经理Apurva Joshi 撰写. 您有多少次在半夜被叫醒去解决一个仅需重新启动网站即可解决的问题?要是可以自动检测一些状况并自动恢 ...
- Java Date 和 Calendar
Java 语言的Date(日期),Calendar(日历),DateFormat(日期格式)组成了Java标准的一个基本但是非常重要的部分.日期是商业逻辑计算一个关键的部分,所有的开发者都应该能够计算 ...
- Baby Step Gaint Step
给定同余式,求它在内的所有解,其中总是素数. 分析:解本同余式的步骤如下 (1)求模的一个原根 (2)利用Baby Step Giant Step求出一个,使得,因为为素数,所以有唯一解. (3)设, ...
- 增强Delphi.RemObject.DataAbstract的脚本功能:多数据库同时操作
我们知道,通过Schema,一个DataAbstracService对应一个数据库:一个服务器可以包含多个DataAbstracService,从而实现对多个数据库的操作.通过事件处理我们可以在一个D ...