先看Application Fundamentals上的一段话:
Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a user action (such as pressing the BACK key)
从这句话可以知道,当某个activity变得“容易”被系统销毁时,该activity的onSaveInstanceState就会被执行,除非该activity是被用户主动销毁的,例如当用户按BACK键的时候。
注意上面的双引号,何为“容易”?言下之意就是该activity还没有被销毁,而仅仅是一种可能性。这种可能性有哪些?通过重写一个 activity的所有生命周期的onXXX方法,包括onSaveInstanceState和onRestoreInstanceState方法,我们可以清楚地知道当某个activity(假定为activity A)显示在当前task的最上层时,其onSaveInstanceState方法会在什么时候被执行,有这么几种情况:
1、当用户按下HOME键时。
这是显而易见的,系统不知道你按下HOME后要运行多少其他的程序,自然也不知道activity A是否会被销毁,故系统会调用onSaveInstanceState,让用户有机会保存某些非永久性的数据。以下几种情况的分析都遵循该原则
2、长按HOME键,选择运行其他的程序时。
3、按下电源按键(关闭屏幕显示)时。
4、从activity A中启动一个新的activity时。
5、屏幕方向切换时,例如从竖屏切换到横屏时。
在屏幕切换之前,系统会销毁activity A,在屏幕切换之后系统又会自动地创建activity A,所以onSaveInstanceState一定会被执行
总而言之,onSaveInstanceState的调用遵循一个重要原则,即当系统“未经你许可”时销毁了你的activity,则 onSaveInstanceState会被系统调用,这是系统的责任,因为它必须要提供一个机会让你保存你的数据(当然你不保存那就随便你了)。
至于onRestoreInstanceState方法,需要注意的是,onSaveInstanceState方法和 onRestoreInstanceState方法“不一定”是成对的被调用的,onRestoreInstanceState被调用的前提是,activity A“确实”被系统销毁了,而如果仅仅是停留在有这种可能性的情况下,则该方法不会被调用,例如,当正在显示activity A的时候,用户按下HOME键回到主界面,然后用户紧接着又返回到activity A,这种情况下activity A一般不会因为内存的原因被系统销毁,故activity A的onRestoreInstanceState方法不会被执行
另外,onRestoreInstanceState的bundle参数也会传递到onCreate方法中,你也可以选择在onCreate方法中做数据还原
分享到:
相关推荐
主要介绍了Android onSaveInstanceState和onRestoreInstanceState触发的时机的相关资料,需要的朋友可以参考下
No boilerplate code like onSaveInstanceState or onRestoreInstanceState any more. Getting started Just add the @AutoRestore annotation to your fields that need to be saved and restored in Activities, ...
"View" code and "Screen Code" such as onCreate,onCreateView, onViewCreated or onSaveInstanceState, onRestoreInstanceState.onActivityResult etc. It makes hard to see each "View Logic" and "Screen Logic...
主要介绍了Android中用onSaveInstanceState保存Fragment状态的方法,onSaveInstanceState可以将数据保存在Fragment或Activity中,需要的朋友可以参考下
简单的订单安卓程序,其中包括OnClickListener,TextWatcher,onSaveInstanceState,onRestoreInstanceState,onConfigurationChanged的用法
Android 中 onSaveInstanceState()使用方法详解 覆盖onSaveInstanceState方法,并在onCreate中检测savedInstanceState和获取保存的值 @Override protected void onSaveInstanceState(Bundle outState) { outState...
次就是状态的保存和恢复(onSaveInstanceState onRestoreInstanceState), 以及Activity之间的跳转和数据传输(intent)。 Activity中常用的函数有SetContentView() findViewById() finish() startActivity(),其...
BaseSavedState是View的一个内部静态类,他实现了Parcelable接口,会把控件的属性(如selStart)序列化到Parcelable容器,这个内部类在View的onSaveInstanceState()方法里面以及onRestoreInstanceState(()方法里面...
为了实现看起来像弹出窗口的活动,我必须获取显示器的宽度和高度并将其加载到DisplayMetric上,此外,我还尝试学习使用onSaveInstanceState和onRestoreInstanceState来保持游戏状态如果设备旋转。 面板的UI元素由九...
使用onSaveInstanceState / onRestoreInstanceState重新创建活动。 选择预告片后,应用程序会使用意图启动预告片。 保存旋转时细节活动滚动视图的滚动位置。 可选功能 存储电影海报,剧情简介,用户评分和发行...
通过这个例子描述onSaveInstanceState何时调用。非常简单的小例子。
切换横竖屏,如何保存数据
Android Camera 三星BUG : 最近在Android项目中使用拍照功能 , 其它型号的手机运行成功了 唯独在三星的相机上遇到了bug . BUG具体体现为 : (1) 摄像头拍照后图片数据不一定能返回 ; onActivityResult的data为空 (2...
app: 不必关心当前进度状态的保存, ProgressBar 已经在onSaveInstanceState()和 onRestoreInstanceState(Parcelable state)中帮我们写好了。定制性很强,可以设置两种风格的进度条,设置进度条的颜色和进度文本...
android-ts-tech-test活动SolutionListActivity-显示从的提要中获取的“解决方案”项... 方向更改-可以使用onSaveInstanceState()和onRestoreInstanceState()更好地管理两次方向更改之间的状态。 特别是在活动需要
1 Android build system 4 1.1 Basic 4 1.2 Executable 6 ...30.3 你后台的Activity被系统回收怎么办:onSaveInstanceState 115 30.4 调用与被调用:我们的通信使者Intent 116 31 编译Native C的helloworld 118
该文档介绍了我在android studio开发中使用的一些常用和实用的技术,包括intent 广播机制,runOnUiThread,AlertDialog显示自定义布局文件问题,横竖屏切换保存状态onSaveInstanceState