zoukankan      html  css  js  c++  java
  • onSaveInstanceState & onRestoreInstanceState

    一、onSaveInstanceState

    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.

    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 called, this method will occur before onStop(). There are no guarantees about whether it will occur before or after onPause().

    tip:

    In situations where the system needs more memory it may kill paused processes to reclaim resources. Because of this, you should be sure that all of your state is saved by the time you return from this function. In general onSaveInstanceState(Bundle) is used to save per-instance state in the activity and onStop() is used to store global persistent data (in content providers, files, etc.) 

    二、onRestoreInstanceState

    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). 

    onRestoreInstanceState() is called only when recreating activity after it was killed by the OS. Such situation happen when:

    • orientation of the device changes (your activity is destroyed and recreated)
    • there is another activity in front of yours and at some point the OS kills your activity in order to free memory (for example). Next time when you start your activity onRestoreInstanceState() will be called.

    In contrast: if you are in your activity and you hit Back button on the device, your activity is finish()ed (i.e. think of it as exiting desktop application) and next time you start your app it is started "fresh", i.e. without saved state because you intentionally exited it when you hit Back.

    Other source of confusion is that when an app loses focus to another app onSaveInstanceState() is called but when you navigate back to your app onRestoreInstanceState() may not be called. This is the case described in the original question, i.e. if your activity was NOT killed during the period when other activity was in front onRestoreInstanceState() will NOT be called because your activity is pretty much "alive".

    三、案例实验

    • 第一次进入应用

      02-09 22:47:39.260: I/System.out(2991): ActivityA onCreate
      02-09 22:47:39.610: I/System.out(2991): ActivityA onStart
      02-09 22:47:39.610: I/System.out(2991): ActivityA onResume

    • 自动灭屏(锁屏)

      02-09 22:47:39.630: I/System.out(2991): ActivityA onPause
      02-09 22:47:39.770: I/System.out(2991): ActivityA onSaveInstanceState
      02-09 22:47:39.770: I/System.out(2991): ActivityA onStop

    • 按POWER键亮屏

      02-09 22:47:50.100: I/System.out(2991): ActivityA onRestart
      02-09 22:47:50.100: I/System.out(2991): ActivityA onStart
      02-09 22:47:50.140: I/System.out(2991): ActivityA onResume

    • 按POWER键灭屏

      02-09 22:49:40.860: I/System.out(2991): ActivityA onPause
      02-09 22:49:40.870: I/System.out(2991): ActivityA onSaveInstanceState
      02-09 22:49:40.870: I/System.out(2991): ActivityA onStop

    • 按BACK键退出

      02-09 22:50:14.650: I/System.out(2991): ActivityA onPause
      02-09 22:50:15.650: I/System.out(2991): ActivityA onStop
      02-09 22:50:15.650: I/System.out(2991): ActivityA onDestroy

    • 按HOME键回到launcher

      02-09 22:51:25.420: I/System.out(2991): ActivityA onPause
      02-09 22:51:26.090: I/System.out(2991): ActivityA onSaveInstanceState
      02-09 22:51:26.090: I/System.out(2991): ActivityA onStop

    • 长按HOME键点击最近使用的程序回到应用

      02-09 22:51:48.910: I/System.out(2991): ActivityA onRestart
      02-09 22:51:48.910: I/System.out(2991): ActivityA onStart
      02-09 22:51:48.910: I/System.out(2991): ActivityA onResume

    • 从ActivityA中点击进入ActivityB

      02-09 22:53:42.170: I/System.out(3158): ActivityA onPause
      02-09 22:53:42.210: I/System.out(3158): ActivityB onCreate
      02-09 22:53:42.670: I/System.out(3158): ActivityA onSaveInstanceState
      02-09 22:53:42.670: I/System.out(3158): ActivityA onStop

    • 从ActivityB按BACK键退回ActivityA

      02-09 22:54:49.320: I/System.out(3158): ActivityA onRestart
      02-09 22:54:49.320: I/System.out(3158): ActivityA onStart
      02-09 22:54:49.320: I/System.out(3158): ActivityA onResume

    • 在ActivityA中点击执行finish()方法

      02-09 22:55:28.110: I/System.out(3158): ActivityA onPause
      02-09 22:55:28.700: I/System.out(3158): ActivityA onStop
      02-09 22:55:28.700: I/System.out(3158): ActivityA onDestroy

    • 旋转屏幕

      02-09 22:59:28.030: I/System.out(3158): ActivityA onPause
      02-09 22:59:28.030: I/System.out(3158): ActivityA onSaveInstanceState
      02-09 22:59:28.030: I/System.out(3158): ActivityA onStop
      02-09 22:59:28.030: I/System.out(3158): ActivityA onDestroy
      02-09 22:59:28.050: I/System.out(3158): ActivityA onCreate
      02-09 22:59:28.220: I/System.out(3158): ActivityA onStart
      02-09 22:59:28.220: I/System.out(3158): ActivityA onRestoreInstanceState
      02-09 22:59:28.230: I/System.out(3158): ActivityA onResume

    • 在ActivityA中点击执行System.exit(0)
      no log

    四、总结

    onSaveInstanceState用于保存与界面有关的属性以便回到下次该界面时恢复状态,在当前Activity非“自愿”退出时调用,如GC回收内存、按HOME键 ,而BACK键与finish方法属于正常的退出界面的方法;而onRestoreInstanceState出现的场景更加苛刻,只有非手动直接造成Activity退出(暂时理解为执行了onDestory,有不对之处请指出),如旋转屏幕时、GC回收内存后,再次进入该Activity时,会调用该方法。

  • 相关阅读:
    洛谷 P1284 三角形牧场WD
    luogu P3817 小A的糖果
    P3374 【模板】树状数组 1
    线程与threading模块
    socketserver模块
    python 粘包问题及解决方法
    python 网络编程
    类的进阶四 反射和内置方法
    python hashlib模块 logging模块 subprocess模块
    类的进阶三
  • 原文地址:https://www.cnblogs.com/hackerkevin/p/4282715.html
Copyright © 2011-2022 走看看