`
bolutes
  • 浏览: 869486 次
文章分类
社区版块
存档分类
最新评论

API Demo Snake代码分析二 onSaveInstanceState和onRestoreInstanceState分析

 
阅读更多

在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()onRestart()onStart()onResume()onPause()onStop()onDestroy()
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()
Called after your activity has been stopped, prior to it being started again.

Always followed byonStart()

No 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()
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()
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()
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()
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>
Called to retrieve per-instance state from an activity before being killed so that the state can be restored inonCreate(Bundle)oronRestoreInstanceState(Bundle)(theBundlepopulated by this method will be passed to both).

protected voidonSaveInstanceState(BundleoutState)

Since:API Level 1

Called to retrieve per-instance state from an activity before being killed so that the state can be restored inonCreate(Bundle)oronRestoreInstanceState(Bundle)(theBundlepopulated 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生命周期回调的一部分,而后者并不能保证每次一定调用


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics