当前位置:网站首页>Calls to onsaveinstancestate and onrestoreinstancestate methods
Calls to onsaveinstancestate and onrestoreinstancestate methods
2022-07-27 01:08:00 【Roy_ chen7】
Only when Activity When it ends in an abnormal state , The system will call onSaveInstance Method , Used to save the current Activity The state of , So that the subsequent reconstruction is abnormally ended Activity Called when the onRestoreInstatnceState Methods to restore the scene .
onSaveInstance The method is only in Activity By End of exception Called when , And it is in onStop Before .onSaveInstance Method will save the data in Bundle In the object , When it ends abnormally Activity After being rebuilt , The system will call onRestoreInstanceState Method , And put this Bundle Objects are also passed to onRestoreInstanceState and onCreate Method . therefore onRestoreInstanceState Method can be used to recovery Activity The state of .onRestoreInstanceState Method is called at onStart after .
that , say concretely onSaveInstance When will the method be called ?
I also mentioned above onSaveInstance The method is in onstop Before execution , That is, it will be called when the user switches to the desktop or other applications .onSaveInstance The method is only in Activity Called when the exception ends , In fact, when your application may end abnormally , This method will be called .
The following are all cases where the application may end abnormally , At this time, the system will call onSaveInstance:
1、 User presses home Key to switch to the desktop
2、 Switch to another application
3、 Press the power button / Breath screen
4、 Horizontal and vertical screen switching
Life cycle of horizontal and vertical screen switching :onPause()->onSaveInstanceState()->onStop()->onDestroy()->onCreate()->onStart()->onRestoreInstanceState->onResume()
It can be seen that the horizontal and vertical screen switching experience Activity Destruction and reconstruction of . If we don't want horizontal and vertical screens to cause Activity Destruction and reconstruction of , We can do that :
stay AndroidManifest Of documents Activity Specify the following properties in :
android:configChanges = "orientation| screenSize"In this case, when the application switches between horizontal and vertical screens , You don't go through Activity Destroyed and rebuilt , Instead, call back the following methods :
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
onRestoreInstatnceState Method is used for Activity It is called to restore the scene when it is created again after the exception . But here's the thing ,onRestoreInstanceState The method is not necessarily and onSaveInstance Methods appear in pairs ,onSaveInstance The method is to judge in the system Activity It may be called when it ends abnormally , and onRestoreInstance The method is to have Activity It can only be called when it is really destroyed by the exception and rebuilt . For example, the user first Activity a Switch to Activity b, Then immediately return to Activity a, Because from a Switch to b When , The system judges that it may be destroyed abnormally , So I call onSaveInstance Method , But because Activity Not really destroyed , So from Activity b Back to Activity a When ,Activity a Not invoke onRestoreInstance.onRestoreInstance The call to is based on Activity In fact, it was destroyed abnormally .
in addition , Because of the call onSaveInstanceState Method Bundle Objects will be passed to onCreate and onRestoreInstanceState Method , So it can also be in onCreate Method to recover data .onCreate and onRestoreInstanceState Method recovery Activity The difference is that :onRestoreInstanceState Callback indicates Bundle Object is not empty , Don't add non empty judgment , and onCreate Medium requires non empty judgment , Therefore, it is generally recommended to use onRestoreInstanceState Method to recover data .
边栏推荐
- DataNode Decommision
- Flink面试常见的25个问题(无答案)
- MySQL索引优化:哪些情况下需要建立索引(适合构建索引的几种情况)
- 进入2022年,移动互联网的小程序和短视频直播赛道还有机会吗?
- [WUSTCTF2020]CV Maker
- 视频类小程序变现的最短路径:从带货到品牌营销
- 腾讯升级视频号小程序直播功能,腾讯持续推广直播的底气是这项叫视立方(MLVB)的技术
- Flink1.11 write MySQL test cases in jdcb mode
- 基于Flink实时项目:用户行为分析(二:实时流量统计)
- Channel shutdown: channel error; protocol method: #method<channel. close>(reply-code=406, reply-text=
猜你喜欢

Uni-app 小程序 App 的广告变现之路:Banner 信息流广告

Flink's fault tolerance mechanism (checkpoint)

The difference between forward and redirect

Flink checkpoint源码理解

Flink1.11 intervalJoin watermark生成,状态清理机制源码理解&Demo分析

Flink 1.15本地集群部署Standalone模式(独立集群模式)
![[watevrCTF-2019]Cookie Store](/img/24/8baaa1ac9daa62c641472d5efac895.png)
[watevrCTF-2019]Cookie Store

Write the changed data in MySQL to Kafka through flinkcdc (datastream mode)

基于Flink实时项目:用户行为分析(二:实时流量统计)
![[WUSTCTF2020]CV Maker](/img/64/06023938e83acc832f06733b6c4d63.png)
[WUSTCTF2020]CV Maker
随机推荐
Deep understanding of golang - closures
[HarekazeCTF2019]encode_ and_ encode
小程序直播、连线直播、直播打赏:腾讯云移动直播组件MLVB多场景直播拓展
Only hard work, hard work and hard work are the only way out C - patient entity class
Spark on yarn's job submission process
腾讯云直播插件MLVB如何借助这些优势成为主播直播推拉流的神助攻?
forward和redirect的区别
select查询题目练习
DataNode Decommision
MySQL index optimization: under what circumstances do you need to build an index (several situations suitable for building an index)
Rabbit学习笔记
Spark源码学习——Data Serialization
Flink 1.15 implements SQL script to recover data from savepointh
李宏毅机器学习(2017版)_P21:卷积神经网络CNN
[hongminggu CTF 2021] write_ shell
Cannot find a valid baseurl for repo: HDP-3.1-repo-1
In depth learning report (2)
MYSQL 使用及实现排名函数RANK、DENSE_RANK和ROW_NUMBER
基于Flink实时项目:用户行为分析(三:网站总浏览量统计(PV))
Essay - I say you are so cute