Activity的生命周期里并没有提到onSaveInstanceState的触发,这个函数提供了为我们在某些情况下保存Activity信息的机会,但需要注意的是这个函数不是什么时候都会被调用的,官方文档解释的比较清楚,特此
翻译一下。
原文出处:android-sdk-windows-1.5_r3/docs/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)

protected void onSaveInstanceState (Bundle outState)

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

 

    在activity被杀掉之前调用保存每个实例的状态,以保证该状态可以在onCreate(Bundle)或者onRestoreInstanceState(Bundle) (传入的Bundle参数是由onSaveInstanceState封装好的)中恢复。这个方法在一个activity被杀死前调用,当该activity在将来某个时刻回来时可以恢复其先前状态。例如,如果activity B启用后位于activity A的前端,在某个时刻activity A因为系统回收资源的问题要被杀掉,A通过onSaveInstanceState将有机会保存其用户界面状态,使得将来用户返回到activity A时能通过onCreate(Bundle)或者onRestoreInstanceState(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.
  
    不要将这个方法和activity生命周期回调如onPause()或onStop()搞混淆了,onPause()在activtiy被放置到背景或者自行销毁时总会被调用,onStop()在activity被销毁时被调用。一个会调用onPause()和onStop(),但不触发onSaveInstanceState的例子是当用户从activity B返回到activity A时:没有必要调用B的onSaveInstanceState(Bundle),此时的B实例永远不会被恢复,因此系统会避免调用它。一个调用onPause()但不调用onSaveInstanceState的例子是当activity B启动并处在activity A的前端:如果在B的整个生命周期里A的用户界面状态都没有被破坏的话,系统是不会调用activity A的onSaveInstanceState(Bundle)的。
 
    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().

 

      默认的实现负责了大部分UI实例状态(的保存),采用的方式是调用UI层上每个拥有id的view的onSaveInstanceState() ,并且保存当前获得焦点的view的id(所有保存的状态信息都会在默认的onRestoreInstanceState(Bundle)实现中恢复)。如果你覆写这个方法来保存额外的没有被各个view保存的信息,你可能想要在默认实现过程中调用或者自己保存每个视图的所有状态。如果被调用,这个方法会在onStop()前被触发,但系统并不保证是否在onPause()之前或者之后触发。



很多不明白Activity类中包含的onSaveInstanceState和onRestoreInstanceState有什么用,首先声明下使用这两个方法时一定要注意情况和了解Activity的生命周期,否则有的时候  onSaveInstanceState和onRestoreInstanceState 可能不会被触发,虽然他们都是Activity的重写方法。(文/Android开发网)

他们比较常用到的地方是 Sensor、Land和Port布局的自动切换,过去Android开发网曾经说过解决横屏和竖屏切换带来的数据被置空或者说onCreate被重复调用问题,其实Android提供的onSaveInstanceState方法可以保存当前的窗口状态在即将布局切换前或当前Activity被推入历史栈,其实布局切换也调用过onPause所以被推入Activity的history stack,如果我们的Activity在后台没有因为运行内存吃紧被清理,则切换回时会触发onRestoreInstanceState方法。

这两个方法中参数均为Bundle,可以存放类似 SharedPreferences 的数据,所以使用它们作为当前窗口的状态保存是比较合适的。实际使用代码

  1. @Override
  2.   protected void onSaveInstanceState(Bundle outState){
  3.             outState.putString("lastPath", "/sdcard/android123/cwj/test");
  4.   }


  5. @Override
  6. public void onRestoreInstanceState(Bundle savedInstanceState) {
  7. super.onRestoreInstanceState(savedInstanceState);

  8. String cwjString = savedInstanceState.getString("lastPath");
  9. }

更多相关文章

  1. android使用webview加载H5页面
  2. Android(安卓)HAL 开发 (4)
  3. android gallery 竖直(上下)滑动
  4. Android(安卓)获得软键盘状态
  5. android service 之一 (start service)
  6. Android(安卓)Loader详解二:使用加载器
  7. Android插入USB设备,自动弹出提示运行apk
  8. Android沉浸式状态栏小结
  9. Android(安卓)Tabhost使用方法详解

随机推荐

  1. Android(安卓)编译时出现r cannot be res
  2. 使用drawable资源
  3. Android(安卓)dumpsys命令学习小记
  4. android聊天,存储聊天记录sqlite
  5. Android(安卓)文件存放路径
  6. Android(安卓)Studio中AVD SDk找不到的解
  7. android读取原始的xml文件
  8. Android(安卓)NFS 文件系统
  9. Android消息机制(三):Looper
  10. Android实现搜索保存历史记录功能