android onSaveInstanceState重载


/**
 * Called to retrieve per-instance state from an activity before being killed
 * so that the state can be restored in {@link #onCreate} or
 * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method
 * will be passed to both).
 *
 * <p>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 {@link #onCreate} or {@link #onRestoreInstanceState}.
 *
 * <p>Do not confuse this method with activity lifecycle callbacks such as
 * {@link #onPause}, which is always called when an activity is being placed
 * in the background or on its way to destruction, or {@link #onStop} which
 * is called before destruction.  One example of when {@link #onPause} and
 * {@link #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 {@link #onSaveInstanceState}
 * on B because that particular instance will never be restored, so the
 * system avoids calling it.  An example when {@link #onPause} is called and
 * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
 * the system may avoid calling {@link #onSaveInstanceState} 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.
 *
 * <p>The default implementation takes care of most of the UI per-instance
 * state for you by calling {@link android.view.View#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
 * {@link #onRestoreInstanceState}).  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.
 *
 * <p>If called, this method will occur before {@link #onStop}.  There are
 * no guarantees about whether it will occur before or after {@link #onPause}.
 *
 * @param outState Bundle in which to place your saved state.
 *
 * @see #onCreate
 * @see #onRestoreInstanceState
 * @see #onPause
 */
protected void onSaveInstanceState(Bundle outState) {
    outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
    Parcelable p = mFragments.saveAllState();
    if (p != null) {
        outState.putParcelable(FRAGMENTS_TAG, p);
    }
    getApplication().dispatchActivitySaveInstanceState(this, outState);
}
/**
    * This is the same as {@link #onSaveInstanceState} but is called for activities
    * created with the attribute {@link android.R.attr#persistableMode} set to
    * <code>persistAcrossReboots</code>. The {@link android.os.PersistableBundle} passed
    * in will be saved and presented in {@link #onCreate(Bundle, PersistableBundle)}
    * the first time that this activity is restarted following the next device reboot.
    *
    * @param outState Bundle in which to place your saved state.
    * @param outPersistentState State which will be saved across reboots.
    *
    * @see #onSaveInstanceState(Bundle)
    * @see #onCreate
    * @see #onRestoreInstanceState(Bundle, PersistableBundle)
    * @see #onPause
    */
   public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
       onSaveInstanceState(outState);
   }

也就是重载两个参数的方法

Paste_Image.png

只有这么设置的时候才会调用,

在开发中由于代码提示直接复写两个参数的方法,状态未保存,跟踪进去发现原来是重写方法的不正确,坑爹

1、PersistableBundle outPersistentState 保存的数据,关机开机还是有的,当然需要设置android:persistableMode="persistAcrossReboots"
2、Bundle outState 应用关闭就没了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android Studio JNI流程首先在java代码声明本地方法 用到native关键字 本地方法不用去实现...
    MigrationUK阅读 11,977评论 7 123
  • iOS网络架构讨论梳理整理中。。。 其实如果没有APIManager这一层是没法使用delegate的,毕竟多个单...
    yhtang阅读 5,332评论 1 23
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,759评论 0 17
  • 1.什么是Activity?问的不太多,说点有深度的 四大组件之一,一般的,一个用户交互界面对应一个activit...
    JoonyLee阅读 5,787评论 2 51
  • 顺友,是我要好的兄弟,其实我们认识是因为我们是大小姨夫。所谓的大小姨夫就是我老婆的姐姐是他老婆,关系好了我们...
    落雨坡阅读 496评论 0 1