注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

民主与科学

独立之人格,自由之思想

 
 
 

日志

 
 

关于onSaveInstanceState(Bundle)和onRestoreInstanceState(Bundle)  

2010-05-26 17:30:27|  分类: Android基础 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Activity 的方法onSaveInstanceState(Bundle)和onRestoreInstanceState(Bundle))用于处理Activity 重生 时状态的保存和恢复。

protected void onSaveInstanceState (Bundle outState)

Since: API Level 1

Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) oronRestoreInstanceState(Bundle) (the Bundle 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 via onCreate(Bundle) oronRestoreInstanceState(Bundle).

Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(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 calling onSaveInstanceState() 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 of onRestoreInstanceState(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 before onStop(). There are no guarantees about whether it will occur before or after onPause().

Parameters
outStateBundle in which to place your saved state.
如果onSaveInstanceState要被调用的话,只能保证它在stop之前调用。但它可以在onPause()之前OR之后调用。
注:应该说active要被杀死才调用该函数。但是按back键不会调用。通过finish()结束active也不会调用onSaveInstanceState。因为既然销毁后无法重生,就没必要调用onSaveInstanceState来保存protected                                            
 protected void onRestoreInstanceState (Bundle savedInstanceState)
Since: API Level 1

This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle).

This method is called between onStart() and onPostCreate(Bundle).

Parameters
savedInstanceStatethe data most recently supplied in onSaveInstanceState(Bundle).

注1: savedInstanceState就是在onSaveInstanceState中报存下来的Bundle.
注2:其实也可以在oncreate中读取
onSaveInstanceState中报存下来的Bundle。但是在onRestoreInstanceState中处理也有些方便的地方。至少要好看些吧。




  评论这张
 
阅读(691)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017