文档章节

Activity 通过 Fragment保存大块数据的一种方法。摘自android开发官网

yhchinabest
 yhchinabest
发布于 2015/09/16 01:06
字数 388
阅读 142
收藏 7

http://developer.android.com/guide/topics/resources/runtime-changes.html


Retaining an Object During a Configuration Change


If restarting your activity requires that you recover large sets of data, re-establish a network connection, or perform other intensive operations, then a full restart due to a configuration change might be a slow user experience. Also, it might not be possible for you to completely restore your activity state with the Bundle that the system saves for you with the onSaveInstanceState() callback—it is not designed to carry large objects (such as bitmaps) and the data within it must be serialized then deserialized, which can consume a lot of memory and make the configuration change slow. In such a situation, you can alleviate the burden of reinitializing your activity by retaining a Fragment when your activity is restarted due to a configuration change. This fragment can contain references to stateful objects that you want to retain.

When the Android system shuts down your activity due to a configuration change, the fragments of your activity that you have marked to retain are not destroyed. You can add such fragments to your activity to preserve stateful objects.

To retain stateful objects in a fragment during a runtime configuration change:

  1. Extend the Fragment class and declare references to your stateful objects.

  2. Call setRetainInstance(boolean) when the fragment is created.

  3. Add the fragment to your activity.

  4. Use FragmentManager to retrieve the fragment when the activity is restarted.

For example, define your fragment as follows:

public class RetainedFragment extends Fragment {

   
// data object we want to retain
   
private MyDataObject data;

   
// this method is only called once for this fragment
   
@Override
   
public void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
       
// retain this fragment
        setRetainInstance
(true);
   
}

   
public void setData(MyDataObject data) {
       
this.data = data;
   
}

   
public MyDataObject getData() {
       
return data;
   
}
}

Caution: While you can store any object, you should never pass an object that is tied to the Activity, such as a Drawable, an Adapter, a View or any other object that's associated with a Context. If you do, it will leak all the views and resources of the original activity instance. (Leaking resources means that your application maintains a hold on them and they cannot be garbage-collected, so lots of memory can be lost.)


© 著作权归作者所有

yhchinabest
粉丝 3
博文 48
码字总数 12695
作品 0
武汉
高级程序员
私信 提问
Activity,Fragment状态保存

Activity android:alwaysRetainTaskState 用法 用来标记Activity所在的Task的状态是否总是由系统来保持——“true”,表示总是;“false”,表示在某种情形下允许系统恢复Task 到它的初始化状...

程序袁_绪龙
2014/12/21
753
0
android之Fragment(官网资料翻译)

Fragment要点 Fragment作为Activity界面的一部分组成出现 可以在一个Activity中同时出现多个Fragment,并且,一个Fragment亦可在多个Activity中使用。 在Activity运行过程中,可以添加、移除...

随智阔
2013/05/26
114
0
解决IllegalStateException: Can not perform this

今天使用Fragment的时候,出现了这个错误 IllegalStateException: Can not perform this action after onSaveInstanceState: E/AndroidRuntime(12747): Caused by: java.lang.IllegalState......

今日竹石
2014/03/17
35
0
Android知识点回顾之Fragment

Fragment是在API 11(Android3.0)引入的,为了能够支持适配宽屏设备,提供灵活多变的UI设计。 Fragment是嵌在Activity里面能够交互的用户界面,它的存在必须依赖于Activity,不能独立存在。...

星泉毅
2017/12/03
0
0
解决IllegalStateException: Can not perform this ~~~

今天使用Fragment的时候,出现了这个错误 IllegalStateException: Can not perform this action after onSaveInstanceState: E/AndroidRuntime(12747): Caused by: java.lang.IllegalState......

今日竹石
2014/03/21
177
0

没有更多内容

加载失败,请刷新页面

加载更多

parseint和isNaN用法

本文转载于:专业的前端网站➭parseint和isNaN用法 <!doctype html><html><head><meta charset="utf-8"><title>无标题文档</title></head><body><script> var a='12'; alert......

前端老手
32分钟前
4
0
Kylin 精确去重在用户行为分析中的妙用

作者:史少锋,Apache Kylin committer & PMC,2019/10/11 在上次文章《如何在 1 秒内做到大数据精准去重》中,我们介绍了 Apache Kylin 为什么要支持大数据集上的精确去重,以及基于 Bitmap...

ApacheKylin
43分钟前
3
0
学习记录(二) es6基本语法(rest参数,模板化,axios模块,拦截器)

日常学习记录 模块化:把一个大文件分成多个小文件,按照一定规范进行拼接 es5写法: 导出:module.exports = 数据 导入:require("路径") /路径未添加后缀名时 //默认添加.js //把路径作为文件名...

Pole丶逐
47分钟前
3
0
以程序员的角度怎么购买一台「性价比高的电视」

前俩天有小伙伴在我的文章下留言,说能否把 【国内电视机都介绍一下】,今天我已在TV端开发多年的程序员的角度。谈谈已程序员的角度如何购买一台性价比高的电视。 国内大的电视机品牌介绍 长...

我们都很努力着
50分钟前
2
0
PhotoShop 色调:理解直方图/RGB通道信息

一、直方图:图表的形式,展示图像像素分布的情况 1.平均值:表示平均亮度 2.标准偏差值:表示亮度值范围内的中间值 3.像素: 表示用于计算直方图的像素总数 4.色阶:显示指针下面的区域亮度...

东方墨天
56分钟前
7
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部