【Android Developers Training】 18. 重新创建一个Activity
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好。
原文链接:http://developer.android.com/training/basics/activity-lifecycle/recreating.html
在应用被正常使用的情况下,会有一些场景发生并使得你的activity被销毁,比如:当用户按下了返回键,或者你的activity调用了finish()方法将自己销毁。当你的activity处于停止态且已经很久没有被使用的情况下,系统也会将它销毁。还有一种情况是处于前台的activity需要更多的资源,此时系统必须将一部分后台进程停止来释放出一些内存空间。
当你的activity因为用户按下了返回键或者自己调用了finish()将自己销毁时,该Activity的实例将不再存在,因为导致其被销毁的行为暗示了这个activity已经不再需要了。然后如果是由于系统资源紧张而被迫将Activity销毁(非正常的应用行为),那么虽然Activity的实例已经不再了,当用户回到这个Activity时,系统仍然会记得它其实是存在的。此时,系统会利用该Activity被销毁时所保存的描述其状态的数据集,来创建一个新的Activity实例。这些系统用来恢复之前状态的数据被称作为“实例状态(instance state)”,它实质上是一个存储于一个Bundle对象的键值对数据集合。
Caution:
每次用户旋转了屏幕后,你的activity都会被销毁和重新创建。这样做的原因是因为屏幕的旋转会导致屏幕参数的变化(长和宽互换),此时你的activity可能需要加载一些可选的资源(比如针对于横屏或竖屏下的布局)
默认的,系统使用Bundle实例状态来保存你的activity布局中每个View对象的信息(比如在EditText对象中输入的文字)。所以,如果你的activity实例被销毁和重新创建,布局的状态会恢复到之前的状态,且你不需要为此编写代码。然而,你的activity可能会有更多你希望存储的状态信息,比如一些用来追踪用户进度的成员变量。
Note:
为了让Android系统恢复activity中的view状态,每个View必须有一个独有的ID,这个ID由属性字段“
android:id”提供。
为了存储更多关于activity状态的数据,你必须覆写onSaveInstanceState()回调函数。当用户离开你的activity时系统会调用该方法,并且传递给他一个Bundle对象,存储数据以防activity被意外地销毁了。之后如果系统必须重新创建这个activity实例,它会把相同的Bundle对象传递给onRestoreInstanceState()方法和onCreate()方法。

图1. 当系统要停止你的activity时,它会调用onSaveInstanceState()方法(1),所以你可以指定你先要存储的额外数据,当activity被重新创建时会用到它们。如果activity被销毁了同时这个activity实例需要重新创建,系统会将(1)中定义的数据传递给onCreate()方法(2),以及onRestoreInstanceState()方法(3)
一). 保存你的Activity状态
当你的activity将要停止,系统会调用onSaveInstanceState()方法这样你的activity会通过键值对的形式将状态信息保存起来。这个方法的默认实现会保存activity的view状态,比如一个EditText控件内的文本信息或者ListView当前滑动到的位置信息。
为了为你的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:
必须调用父类的onSaveInstanceState(),这样默认的实现会保存View的状态
二). 恢复你的Activity状态
当你的Activity在之前被销毁后需要被重新创建时,你可以通过系统传递给Activity的Bundle对象来恢复之前的状态。onRestoreInstanceState()方法和onCreate()方法都会接受到相同的Bundle对象,其中包含了实例的状态信息。
因为不管是系统在创建一个新的实例还是重新创建一个旧的,onCreate()都会被系统调用,所以在你尝试读取Bundle之前,必须检查它是否是null。如果是null,那么系统会创建一个新的activity实例,而不是进行恢复。
下面就是你将如何在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
}
...
}
除了在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);
}
Caution:
必须调用父类的onRestoreInstanceState(),这样默认的实现会恢复View的状态
如果想要学习关于在运行时由于重启事件导致activity被重新创建(如旋转了屏幕),可以阅读:Handling Runtime Changes
【Android Developers Training】 18. 重新创建一个Activity的更多相关文章
- 【Android Developers Training】 1. 创建一个Android项目工程
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 95. 创建一个同步适配器
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 15. 启动一个Activity
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 21. 创建一个可变动的UI
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 20. 创建一个Fragment
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 94. 创建一个空内容提供器(Content Provider)
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 93. 创建一个空验证器
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 3. 构建一个简单UI
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 106. 创建并检测地理围栏
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
随机推荐
- [刷题]算法竞赛入门经典(第2版) 5-5/UVa10391 - Compound Words
题意:问在一个词典里,那些单词是复合词,即哪些单词是由两个单词拼出来的. 渣渣代码:(Accepted, 30ms) //UVa10391 - Compound Words #include<i ...
- springmvc缓存和mybatis缓存
首先要有一个搭建好的ssm框架,笔者使用的是基于maven搭建的ssm框架. 加入springmvc缓存: 导入相关依赖包: <dependency> <groupId>org ...
- 编写自己的一个简单的web容器(一)
在之前的博客中我更大家说过Http协议是对tcp协议的封装,其底层还是使用tcp协议来进行数据传出的,浏览器实际上就是一个Socket客户端,今天呢我们就开始着手利用ServerSocket来编写一个 ...
- jsp中的request.setAttribute的使用
1.第一个jsp页面 <form id="form1" method="post" action="first.jsp"> &l ...
- mysqldump备份还原mysql
本文实现在mysql 5.7 解压版为例子 1.在window上简单试下一个例子 1.使用管理员权限打开cmd命名行,并切换到mysqldump执行程序下
- Mysql5.7忘记root密码及修改root密码的方法
Mysql 安装成功后,输入 mysql --version 显示版本如下 mysql Ver 14.14 Distrib 5.7.13-6, for Linux (x86_64) using 6.0 ...
- AtomicInteger的使用
JDK API 1.7相关介绍 可以用原子方式更新的 int 值.有关原子变量属性的描述,请参阅 java.util.concurrent.atomic 包规范.AtomicInteger 可用在应用 ...
- Principal Components Regression, Pt.1: The Standard Method
In this note, we discuss principal components regression and some of the issues with it: The need fo ...
- 移动端车牌识别——可以嵌入智能手机系统里的新OCR识别技术
移动端车牌识别技术,是在OCR光学字符识别技术的基础上研发的用来识别汽车号牌特征信息的图像识别技术.在国内,该项技术由北京易泊时代携手清华大学成功地将"国家863计划"项目成果-- ...
- tornado之文件上传的几种形式form,伪ajax(iframe)
1直接form提交给后台处理 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...