zoukankan      html  css  js  c++  java
  • Android save state

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

    Usually you restore your state in onCreate(). It is possible to restore it in onRestoreInstanceState() as well, but not very common. (onRestoreInstanceState() is called after onStart(), whereas onCreate() is called before onStart().

    Use the put methods to store values in onSaveInstanceState():

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

    And restore the values in onCreate():
    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 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".

    All in all, as stated in the documentation for onRestoreInstanceState():

    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 override onRestoreInstanceState() unless you are subclassingActivity and it is expected that someone will subclass your subclass.

     

  • 相关阅读:
    【前端进阶】VUE高性能组件引用
    「前端进阶」高性能渲染十万条数据(虚拟列表) (自己修改版本)
    页面缓存、离线存储技术localforage(案例篇)
    页面缓存、离线存储技术localforage(介绍篇)
    websocket快速搭建(node+websocket)
    一款程序员的杀手级应用:TabNine代码补全工具
    如何把es6的代码转成es5,ECMAScript 2015+代码转换神器——Babel
    如何使用echarts画一个简单k线图
    深入浅出理解 . 深拷贝 . 浅拷贝
    JS高级一看就懂什么是原型链
  • 原文地址:https://www.cnblogs.com/qiengo/p/2710335.html
Copyright © 2011-2022 走看看