在Snake贪吃蛇项目Snake.java这个Activity中
在onSaveInstanceState中保存了数据到Bundle中,而在onCreate中恢复了这个数据
在API中Activity的介绍中说:
In general the movement through an activity's lifecycle looks like this:
Method
Description
Killable?
Next
onCreate()
Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.
Always followed byonStart() .
|
No |
onStart()
|
|
onRestart()
Called after your activity has been stopped, prior to it being started again.
Always followed byonStart()
|
No |
onStart()
|
onStart()
Called when the activity is becoming visible to the user.
Followed byonResume() if the activity comes to the foreground, oronStop() if it becomes hidden.
|
No |
onResume() oronStop()
|
|
onResume()
Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.
Always followed byonPause() .
|
No |
onPause()
|
onPause()
Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because
the next activity will not be resumed until this method returns.
Followed by eitheronResume() if the activity returns back to the front, oronStop() if it becomes invisible to the user.
|
Pre-HONEYCOMB
|
onResume() or
onStop()
|
onStop()
Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one
is being destroyed.
Followed by eitheronRestart() if this activity is coming back to interact with the user, oronDestroy() if this activity is going away.
|
Yes
|
onRestart() or
onDestroy()
|
onDestroy()
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone calledfinish() on
it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with theisFinishing() method. |
Yes
|
nothing
|
Note the "Killable" column in the above table -- for those methods that are marked as being killable, after that method returns the process hosting the activity may killed by the systemat any timewithout another line of its code being executed. Because
of this, you should use theonPause()
method to write any persistent data (such as
user edits) to storage. In addition, the methodonSaveInstanceState(Bundle)
is
called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, to be later received inonCreate(Bundle)
if
the activity needs to be re-created. See theProcess Lifecyclesection for more information on how the lifecycle of a process is tied
to the activities it is hosting. Note that it is important to save persistent data inonPause()
instead
ofonSaveInstanceState(Bundle)
because the latter is not part of the lifecycle callbacks, so will not
be called in every situation as described in its documentation.
<nobr>void</nobr> |
<nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)" style="color:rgb(0,102,153); text-decoration:none">onSaveInstanceState</a></span>(<a href="http://developer.android.com/reference/android/os/Bundle.html" style="color:rgb(0,102,153); text-decoration:none">Bundle</a>outState)</nobr>
|
protected voidonSaveInstanceState(BundleoutState)
Called to retrieve per-instance state from an activity before being killed so that the state can be restored inonCreate(Bundle)
oronRestoreInstanceState(Bundle)
(theBundle
populated
by this method will be passed to both).
This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity
A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored viaonCreate(Bundle)
oronRestoreInstanceState(Bundle)
.
Do not confuse this method with activity lifecycle callbacks such asonPause()
, which
is always called when an activity is being placed in the background or on its way to destruction, oronStop()
which
is called before destruction. One example of whenonPause()
andonStop()
is
called and not this method is when a user navigates back from activity B to activity A: there is no need to callonSaveInstanceState(Bundle)
on
B because that particular instance will never be restored, so the system avoids calling it. An example whenonPause()
is
called and notonSaveInstanceState(Bundle)
is when activity
B is launched in front of activity A: the system may avoid callingonSaveInstanceState(Bundle)
on
activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.
The default implementation takes care of most of the UI per-instance state for you by callingonSaveInstanceState()
on
each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation ofonRestoreInstanceState(Bundle)
).
If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself.
If called, this method will occur beforeonStop()
. There are no guarantees about whether
it will occur before or afteronPause()
.
转载:
http://www.cnblogs.com/heiguy/archive/2010/10/30/1865239.html
从这句话可以知道,当某个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方法中做数据还原
备注:应当特别注意这句话
Note that it is important to save persistent data inonPause()
instead ofonSaveInstanceState(Bundle)
because
the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.
保存永久数据应该优先用onPause()而不是onSaveInstanceState(),因为前者是Activity生命周期回调的一部分,而后者并不能保证每次一定调用
分享到:
相关推荐
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, ...
主要介绍了Android onSaveInstanceState和onRestoreInstanceState触发的时机的相关资料,需要的朋友可以参考下
"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...
简单的订单安卓程序,其中包括OnClickListener,TextWatcher,onSaveInstanceState,onRestoreInstanceState,onConfigurationChanged的用法
切换横竖屏,如何保存数据
BaseSavedState是View的一个内部静态类,他实现了Parcelable接口,会把控件的属性(如selStart)序列化到Parcelable容器,这个内部类在View的onSaveInstanceState()方法里面以及onRestoreInstanceState(()方法里面...
通过这个例子描述onSaveInstanceState何时调用。非常简单的小例子。
为了实现看起来像弹出窗口的活动,我必须获取显示器的宽度和高度并将其加载到DisplayMetric上,此外,我还尝试学习使用onSaveInstanceState和onRestoreInstanceState来保持游戏状态如果设备旋转。 面板的UI元素由九...
项目2-热门电影第二阶段-预告片,评论和收藏夹 Android应用程序可通过来显示热门电影或收视率最高的电影的列表以及电影其他详细信息。 用户故事 用户可以通过自己喜欢的浏览器查看和播放预告片。 用户可以阅读所选...
次就是状态的保存和恢复(onSaveInstanceState onRestoreInstanceState), 以及Activity之间的跳转和数据传输(intent)。 Activity中常用的函数有SetContentView() findViewById() finish() startActivity(),其...
android 内存被回收是一个开发者的常见问题。当我们跳转到一个二级界面,或者切换到后台的时候,如果时间过长或者手机的内存不足,当我们再返回这个界面的时候,activity或fragment就会被内存回收。
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 中 onSaveInstanceState()使用方法详解的相关资料,希望通过本文大家能够掌握这部分知识,需要的朋友可以参考下
主要介绍了Android中用onSaveInstanceState保存Fragment状态的方法,onSaveInstanceState可以将数据保存在Fragment或Activity中,需要的朋友可以参考下
app: 不必关心当前进度状态的保存, ProgressBar 已经在onSaveInstanceState()和 onRestoreInstanceState(Parcelable state)中帮我们写好了。定制性很强,可以设置两种风格的进度条,设置进度条的颜色和进度文本...
android-ts-tech-test活动SolutionListActivity-显示从的提要中获取的“解决方案”项... 方向更改-可以使用onSaveInstanceState()和onRestoreInstanceState()更好地管理两次方向更改之间的状态。 特别是在活动需要
异步任务 ... 使用生命周期onSaveInstanceState()和onRestoreInstanceState() 使用片段中的异步任务,在Fragnment中,一个生命周期回调为setRetainInstance(true),在创建片段时将调用此方法。