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 ...
随机推荐
- 转 c#代码访问https服务器以及https的webservice
最近公司做到WebService项目,但是要通过Https调用,自己在网上搜了半天,终于实现了服务端的Https,但是一直没有找到客户端如何实现,今天终于看到这篇文章,随手记录下来. 具体代码如下: ...
- BZOJ 1083: [SCOI2005]繁忙的都市(MST)
裸的最小生成树..直接跑就行了 ---------------------------------------------------------------------- #include<c ...
- BZOJ 1449: [JSOI2009]球队收益( 最小费用最大流)
先考虑假如全部输了的收益. 再考虑每场比赛球队赢了所得收益的增加量,用这个来建图.. --------------------------------------------------------- ...
- Linux 下 Hadoop java api 问题
1. org.apache.hadoop.security.AccessControlException: Permission denied: user=opsuser, access=WRITE, ...
- 5.6.3.8 fromCharCode()方法
String构造函数本身还有一个静态方法:fromCharCode().这个方法的任务是接收一或多个字符编码,然后将它们转换成一个字符.从本质上来看,这个方法与实例方法charCodeAt()执行的是 ...
- 整理一些常用函数库PHP版本
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) { if(function_ex ...
- C-重定向
说实话,第一次接触重定向这一个概念,感觉是那么的神奇简洁不可思议…………………… freopen() 本来应该是打开的是文件指针,但是分配了指针,使她(亲切)指向了标准输入.输出.错误流. 用 法: ...
- TexturePacker
TexturePacker 可以免费申请,希望可以申请到.
- 在Livemedia的基础上开发自己的流媒体客户端
一.背景 二.Livemedia框架介绍 1.总体框架 2.客户端框架 2.1 客户端openRTSP流程 2.2增加一种新的媒体 2.2.1增加媒体的format 2.2.2 新媒体需要考虑的问题 ...
- JavaScript推断E-mail地址是否合法
编写自己定义的JavaScript函数checkEmail(),在该函数中首先推断E-mail文本框是否为空,然后在应用正則表達式推断E-mail地址是否合法,假设不合法提示用户 <script ...