UseonSaveInstanceState() to save the states of the activity and resume it in onCreate()

Usually you restore your state inonCreate(). It is possible to restore it inonRestoreInstanceState()as well, but not very common. (onRestoreInstanceState()is called afteronStart(), whereasonCreate()is called beforeonStart().

Use the put methods to store values inonSaveInstanceState():

protectedvoid onSaveInstanceState(Bundle icicle){  super.onSaveInstanceState(icicle);  icicle.putLong("param", value);}

And restore the values inonCreate():
publicvoid onCreate(Bundle icicle){  if(icicle !=null){    value = icicle.getLong("param");  }}
You do not have to store view states, as they are stored automatically.

onRestoreInstanceState()is calledonly when recreatingactivity after it waskilledby 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 activityonRestoreInstanceState()will be called.

In contrast: if you are in your activity and you hitBackbutton 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 hitBack.

Other source of confusion is that when an app loses focus to another apponSaveInstanceState()is called but when you navigate back to your apponRestoreInstanceState()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 frontonRestoreInstanceState()will NOT be called because your activity is pretty much "alive".

All in all, as stated in the documentation foronRestoreInstanceState():

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

As I read it: There is no reason to overrideonRestoreInstanceState()unless you are subclassingActivityand it is expected that someone will subclass your subclass.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android(安卓)onBackPressed()函数重载失
  2. android应用安全——(数据抓包)跟踪监控and
  3. Android - ReactNative Debug 技巧
  4. Android 设置投影效果
  5. Android网络请求心路历程
  6. 开源项目之Android undergarment
  7. 用cmd 命令更改Android 的默认虚拟机的地
  8. 避免Activity启动时某个控件马上获取焦点
  9. 微信小程序性能分析Trace工具
  10. 【转】 Android(安卓)Action大全