Android菜鸟的成长笔记(15)—— Android中的状态保存探究(下)
原文:Android菜鸟的成长笔记(15)—— Android中的状态保存探究(下)
在上一篇中我们简单了解关于Android中状态保存的过程和原理,这一篇中我们来看一下在系统配置改变的情况下保存数据及恢复数据的过程。
下面我们先来看一个现象:(代码在 Android中状态保存探究(上)中)
先启动应用如下:
打印的Log
再翻转屏幕
打印的Log如下
可以看到每翻转一次屏幕实际上系统会停止原理的activity并销毁然后重新启动一次,在这个过程中会调用onSaveInstanceState方法来保存原来的数据并通过onCreate方法恢复数据。因为要先onDestroy然后才onCreate,所以就会出现一个黑屏(闪屏)的短暂过程。像这种改变整个系统配置的现象叫做ConfigurationChanges.
ConfigurationChange具体有哪些,都代表什么,请看:http://developer.android.com/reference/android/R.attr.html#configChanges
| Constant | Value | Description |
|---|---|---|
mcc |
0x0001 | The IMSI MCC has changed, that is a SIM has been detected and updated the Mobile Country Code. |
mnc |
0x0002 | The IMSI MNC has changed, that is a SIM has been detected and updated the Mobile Network Code. |
locale |
0x0004 | The locale has changed, that is the user has selected a new language that text should be displayed in. |
touchscreen |
0x0008 | The touchscreen has changed. Should never normally happen. |
keyboard |
0x0010 | The keyboard type has changed, for example the user has plugged in an external keyboard. |
keyboardHidden |
0x0020 | The keyboard or navigation accessibility has changed, for example the user has slid the keyboard out to expose it. Note that despite its name, this applied to any accessibility: keyboard or navigation.键盘隐藏 |
navigation |
0x0040 | The navigation type has changed. Should never normally happen.导航 |
orientation |
0x0080 | The screen orientation has changed, that is the user has rotated the device.屏幕翻转 |
screenLayout |
0x0100 | The screen layout has changed. This might be caused by a different display being activated.屏幕布局改变 |
uiMode |
0x0200 | The global user interface mode has changed. For example, going in or out of car mode, night mode changing, etc. |
screenSize |
0x0400 | The current available screen size has changed. If applications don't target at least HONEYCOMB_MR2 then the activity will always handle this itself (the change will not result in a restart). This represents a change in the currently available size, so will change when the user switches between landscape and portrait. |
smallestScreenSize |
0x0800 | The physical screen size has changed. If applications don't target at least HONEYCOMB_MR2 then the activity will always handle this itself (the change will not result in a restart). This represents a change in size regardless of orientation, so will only change when the actual physical screen size has changed such as switching to an external display. |
layoutDirection |
0x2000 | The layout direction has changed. For example going from LTR to RTL. |
fontScale |
0x40000000 | The font scaling factor has changed, that is the user has selected a new global font size.字体大小 |
有时候我们需要在配置改变的时候恢复一些不能够序列化的对象,这时候就需要用onRetainNonConfigurationInstance方法保存,然后在onCreate方法中通过getLastNonConfigurationInstance获取对象。但是要注意的是,保存的对象不要用与context关联的对象,比如View,否则会引起内存泄露。
有没有什么方法在Configuration改变的时候不重新启动Activity,在manifest文件中我们来看一下一个配置:
好吧,先配上orientation值,我们再去Activity中添加一段代码,如下:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("大碗干拌", "调用了onConfiguration onChanged方法");
if(newConfig.equals(Configuration.ORIENTATION_LANDSCAPE)){
Log.i("大碗干拌", "切换到了横屏");
}else if(newConfig.equals(Configuration.ORIENTATION_PORTRAIT)){
Log.i("大碗干拌", "切换到了竖屏");
}
}
这个时候切换屏幕时系统会调用onConfigurationChanged方法,来处理我们自定义的配置改变。而不会去重新启动新的Activity。
Android菜鸟的成长笔记(15)—— Android中的状态保存探究(下)的更多相关文章
- Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上)
原文:[置顶] Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上) 我们在用手机的时候可能会发现,即使应用被放到后台再返回到前台数据依然保留(比如说我们正在玩游戏,突然电话 ...
- Android菜鸟的成长笔记(11)——Android中的事件处理
原文:[置顶] Android菜鸟的成长笔记(11)——Android中的事件处理 Android提供了两种方式来处理事件,一个是基于回调的事件处理,另一个是基于监听的事件处理,举个例子: 基于回调的 ...
- Android菜鸟的成长笔记(17)—— 再看Android中的Unbounded Service
原文:Android菜鸟的成长笔记(17)-- 再看Android中的Unbounded Service 前面已经写过关于startService(Unbounded Service)的一篇文章:&l ...
- Android菜鸟的成长笔记(3)——给QQ登录界面说So Easy
原文:Android菜鸟的成长笔记(3)--给QQ登录界面说So Easy 上一篇:Android菜鸟的成长笔记(2)--第一个Android应用 我们前面已经做了第一个Android应用程序,虽然有 ...
- Android菜鸟的成长笔记(2)——第一个Android应用
原文:Android菜鸟的成长笔记(2)--第一个Android应用 上一篇:Android菜鸟的成长笔记(1)--Anddroid环境搭建从入门到精通 在上一篇Android菜鸟的成长笔记(1)中我 ...
- Android菜鸟的成长笔记(1)——Android开发环境搭建从入门到精通
原文:Android菜鸟的成长笔记(1)--Android开发环境搭建从入门到精通 今天在博客中看到好多Android的初学者对Android的开发环境的搭建不熟悉而导致不能进行学习,所以我决定自己写 ...
- Android菜鸟的成长笔记(13)——异步任务(Async Task)
原文:[置顶] Android菜鸟的成长笔记(13)——异步任务(Async Task) Android的UI线程主要负责处理用户的事件及图形显示,因此主线程UI不能阻塞,否则会弹出一个ANR(App ...
- Android菜鸟的成长笔记(12)——Handler、Loop、MessageQueue
原文:[置顶] Android菜鸟的成长笔记(12)——Handler.Loop.MessageQueue 当一个程序第一次启动时,Android会启动一条主线程(Main Thread),主线程主要 ...
- Android菜鸟的成长笔记(10)——使用Bundle在Activity之间传值
原文:[置顶] Android菜鸟的成长笔记(10)——使用Bundle在Activity之间传值 前面我们了解了如何启动一个Activity,一个Activity在启动另外一个Activity的时候 ...
随机推荐
- c#中的jQuery——HtmlAgilityPack
原文:c#中的jQuery--HtmlAgilityPack c#中是否有javascript中的jQuery类库? jQuery在访问和操作HTML 的DOM的便捷是前端开发工程师的一种福音,在c# ...
- POJ 3267-The Cow Lexicon(DP)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8252 Accepted: 3888 D ...
- 第27本:《学得少却考得好Learn More Study Less》
第27本:<学得少却考得好Learn More Study Less> <学得少却考得好Learn More Study Less>这本书最早是从褪墨网站上看到的,crownc ...
- UpdatePanel Repeater内LinkButton造成页面刷新问题
本意:UpdatePanel1内嵌的Repeater1中带有LinkButton1, 将由LinkButton1触发页面的UpdatePanel2更新,而不需要更新UpdatePanel1,当然也不需 ...
- gdb学习(一个)[再版]
概要 gdb是GNU debugger的缩写,是编程调试工具. 功能 1.启动程序,能够依照用户自己定义的要求随心所欲的执行程序. 2.可让被调试的程序在用户所指定的断点处停住 (断点能够是条件表达式 ...
- SVN的命令解析(感觉不错就转了)
本文链接: http://www.php-oa.com/2008/03/12/svnminglingzailinuxxiadeshiyong.html .将文件checkout到本地目录 svn ch ...
- Mac OS X在建筑Python科学计算环境
经验(比如这篇日志:http://blog.csdn.net/waleking/article/details/7578517).他们推荐使用Mac Ports这种软件来管理和安装全部的安装包.依照这 ...
- Erlang学习: EUnit Testing for gen_fsm
背景:gen_fsm 是Erlang的有限状态机behavior,很实用.爱立信的一位TDD大神写了一篇怎样測试gen_fsm,这个fsm是一个交易系统,负责简单的交易员登陆,插入item,删除ite ...
- Cocos2d-x3.0下一个 Lua与C++打电话给对方
这里谈下Lua与C++如何实现相互通话 原来的连接:http://blog.csdn.net/qqmcy/article/details/26052771 DJLCData.h 实现类 // // D ...
- ActiveMQ相关背景(转)
概述 介绍中间件.MOM.JMS.ActiveMQ,及相互的关系. 中间件 由于业务的不同.技术的发展.硬件和软件的选择有所差别,导致了异构组件或应用并存的局面.要使这些异构的组件协同工作,一个有效的 ...