Android中用onSaveInstanceState保存Fragment状态的方法
作者:SoloHo 时间:2022-05-16 00:39:44
在Fragment里面,利用onSaveInstanceState保存数据,并可在onActivityCreated里面恢复数据。
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
...
if (savedInstanceState != null) {
// Restore the fragment's state here
}
}
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save the fragment's state here
}
在Activity里面,利用onSaveInstanceState保存Fragment实例的数据,并可在onCreate里面恢复数据:
private Fragment myFragment;
public void onCreate(Bundle savedInstanceState) {
...
if (savedInstanceState == null) {
// Instantiate fragment
} else {
// Restore the fragment's instance
myFragment = getSupportFragmentManager().getFragment(
savedInstanceState, "fragment");
}
...
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//Save the fragment's instance
// fragment instance may be null
if (myFragment != null) {
getSupportFragmentManager().putFragment(outState, "fragment", myFragment);
}
}
实例
首先看一个步骤,如果是人为关闭Activity或者Fragment, 比如点击了后退键,主动关闭当前页面,就会走 onPause() --> onStop() --> onDestroy() 而onSaveInstanceState() 并不会被调用. onSaveInstanceState() 只有在系统即将要自动清理销毁Activity或Fragment前才会调用, 比如
1, 由于重力感应 手机从竖屏变为横屏,
2, 手机点击Home键和长按Home键
3, 点击电源键锁屏时
4, 从当前Activity跳到另一个Activity
5, 应用内存不足即将自动销毁时等情况
综合上面的情况,可以看出, onSaveInstanceState() 这个方法适合临时保存一些非永久性的数据.如果要持久化保存数据,就要将操作放在onStop(), onDestroy()这些方法中去.onSaveInstanceState() 适合的是当前Activity或者Fragment一旦被系统自己销毁,应用还能在这之前保存一些必要的数据, 并且用户的操作很快又回到当前这个页面,这个时候数据并没有丢失,还能最大程度的恢复之前状态.这就是这个方法最大的意义.下面上例子做个简单示例:
在Fragment中:
//系统自动销毁Fragment前保存必要的数据
@Override
public void onSaveInstanceState(Bundle outState){
<span style="white-space:pre"> </span>super.onSaveInstanceState(outState);
<span style="white-space:pre"> </span>String content = etCon.getText().toString();
<span style="white-space:pre"> </span>outState.putString("inputCon", content);
}
//恢复数据
@Override
public void onViewStateRestored(Bundle savedInstanceState){
<span style="white-space:pre"> </span>super.onViewStateRestored(savedInstanceState);
<span style="white-space:pre"> </span>//恢复之前输入框的内容
<span style="white-space:pre"> </span>if(savedInstanceState != null){
<span style="white-space:pre"> </span>etCon.setText(savedInstanceState.getString("inputCon", ""));
}
}
在Activity中
//系统自动销毁Activity前保存必要的数据
@Override
public void onSaveInstanceState(Bundle outState){
<span style="white-space:pre"> </span>super.onSaveInstanceState(outState);
String content = etCon.getText().toString();
outState.putString("inputCon", content);
}
//Activity中可在onRestoreInstanceState()方法恢复数据,也可以在onCreate()中恢复,因为Activity一旦被销毁就会重走一次onCreate()开始的正常生命周期
@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
//恢复之前输入框的内容
if(savedInstanceState != null){
etCon.setText(savedInstanceState.getString("inputCon", ""));
}
}
@Override
public void onDestroy(){
super.onDestroy();
//Activity被销毁前可进行最后一次数据的保存,过了这村就没这店了.
}
这里有个重要的点,上面的上面所提及的onSaveInstanceState()方法只有在Activity或Fragment确定要被系统自动清除时,才会被调用,如果不是长期后台应用,页面没有被系统kill掉,对应的Activity或Fragment也就不会调用onSaveInstanceState()这些方法.
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#中WebClient实现文件下载
![](https://img.aspxhome.com/file/2023/5/77275_0s.png)
详解Spring Boot集成MyBatis(注解方式)
![](https://img.aspxhome.com/file/2023/1/64481_0s.jpg)
Java雇员管理小项目
![](https://img.aspxhome.com/file/2023/1/87371_0s.png)
C# goto语句的具体使用
![](https://img.aspxhome.com/file/2023/6/71766_0s.png)
SpringBoot如何读取xml配置bean(@ImportResource)
![](https://img.aspxhome.com/file/2023/8/85888_0s.jpg)
使用@符号让C#中的保留字做变量名的方法详解
![](https://img.aspxhome.com/file/2023/7/82127_0s.png)
Java实现限定时间CountDownLatch并行场景
基于JDK动态代理原理解析
Windows编写jar启动脚本和关闭脚本的操作方法
![](https://img.aspxhome.com/file/2023/8/61298_0s.png)
Android开发之TimePicker控件用法实例详解
![](https://img.aspxhome.com/file/2023/2/106042_0s.png)
Android开发解决字符对齐问题方法
![](https://img.aspxhome.com/file/2023/5/138945_0s.jpg)
Android隐私协议提示弹窗的实现流程详解
![](https://img.aspxhome.com/file/2023/9/106869_0s.png)
C++实现幸运大抽奖(QT版)
![](https://img.aspxhome.com/file/2023/8/124048_0s.jpg)
WinForm使用DecExpress控件中的ChartControl插件绘制图表
![](https://img.aspxhome.com/file/2023/7/111077_0s.png)
Java Lambda表达式与匿名内部类的联系和区别实例分析
Java的锁机制:synchronized和CAS详解
![](https://img.aspxhome.com/file/2023/9/101929_0s.png)
C语言压缩文件和用MD5算法校验文件完整性的实例教程
![](https://img.aspxhome.com/file/2023/6/127556_0s.png)
C#监测IPv4v6网速及流量的实例代码
![](https://img.aspxhome.com/file/2023/8/100228_0s.png)