博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于Activity的onSaveInstanceSate()这个API
阅读量:6263 次
发布时间:2019-06-22

本文共 4056 字,大约阅读时间需要 13 分钟。

hot3.png

####关于onSaveInstanceSate()这个API 参考:

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

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.

The default implementation takes care of most of the UI per-instance state for you by calling 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 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 after onStop() for applications targeting platforms starting with Build.VERSION_CODES.P. For applications targeting earlier platform versions this method will occur before onStop() and there are **no guarantees** about whether it will occur before or after onPause().


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. 比如说 Activity_B在Activity_A里被启动了,在某个时候Activity_A被kill掉以节省资源. 但是Activity_A有一个机会保存当前的状态,这样当下次用户重新回到Activity_A的时候, 之前保存的状态信息就可以被restored通过onCreate(Bundle) 或者onRestoreInstanceState(Bundle).

但是文档同时也提醒了不要把这个方法和onPause混淆. onPause是一定会被调用的当当前的Activity被放到后台或者销毁的时候. onPause和onStop的调用时机和onSaveInstanceState没什么关系. 比如当用户从B navigate back回A的时候,因为这个时候确定B instance will never be restored. 所以就不会调用onSaveInstanceState,但是onPause和onStop肯定会调用. 还有一个例子当onPause()被调用但是不会调用onSaveInstanceState() 一个activity B被唤起在activity A的前面,系统就不会调用A的onSaveInstanceState()方法, if it isn't killed during the lifetime of B. Since the user interface of A will stay intact.

默认行为是系统会调用每个有id的view的View.onsaveInstanceState()方法保存其state. 就是说super.onSaveInstanceState(bundle)干的就是这个事.

protected void onSaveInstanceState(Bundle outState) {        outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());        outState.putInt(LAST_AUTOFILL_ID, mLastAutofillId);        Parcelable p = mFragments.saveAllState();        if (p != null) {            outState.putParcelable(FRAGMENTS_TAG, p);        }        if (mAutoFillResetNeeded) {            outState.putBoolean(AUTOFILL_RESET_NEEDED, true);            getAutofillManager().onSaveInstanceState(outState);        }        getApplication().dispatchActivitySaveInstanceState(this, outState);    }

关于调用时序 在Android P以后(ApiLevel 28),这个方法会在onStop()以后调用. 而在P之前的话保证了在onStop()之前,但是不保证是不是在onPause()之后.

转载于:https://my.oschina.net/tanghaoo/blog/2050962

你可能感兴趣的文章
excel
查看>>
echarts 通过ajax实现动态数据加载
查看>>
python-web-server-tcp1
查看>>
shell脚本入门
查看>>
Management
查看>>
URAL 2048 Histroy(打表+模拟)
查看>>
深入理解String, StringBuffer, StringBuilder的区别(基于JDK1.8)
查看>>
【转】oracle in与exists语句的区别
查看>>
RPC 使用中的一些注意点
查看>>
Django_rest framework 框架介绍
查看>>
Hello world,Hello 2014,Bye 2013
查看>>
python之正则表达式模块
查看>>
BFC和清除浮动
查看>>
笔记:2016-06-04
查看>>
ECSHOP 布局参考图
查看>>
Entity Framework 延伸系列目录
查看>>
Java 代码安全(一) —— 避免用String储存敏感数据
查看>>
制作一个最小Linux系统
查看>>
3个著名加密算法(MD5、RSA、DES)的解析
查看>>
BBS(仿博客园系统)项目05(后台管理功能实现:文章添加、富文本编辑器使用、xss攻击、BeautifulSoup4模块、富文本编辑器上传图片、修改头像)...
查看>>