片段,在onSaveInstanceState上保存大量数据(如何防止TransactionTooLargeException) [英] Fragment, save large list of data on onSaveInstanceState (how to prevent TransactionTooLargeException)

查看:156
本文介绍了片段,在onSaveInstanceState上保存大量数据(如何防止TransactionTooLargeException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,ViewPager中有Fragment.片段包含RecyclerView以及根据用户选择从Web api获取的数据列表.

In my app, I have Fragment which is inside ViewPager. Fragment contains RecyclerView with list of data fetched from web api based on user selection.

在我的Fragment onSaveInstanceState 上,我将列表数据保存到Bunde,以使数据保留配置更改等信息.

On my Fragment onSaveInstanceState I save list data to Bunde, to keep the data on configuration changes etc.

public void onSaveInstanceState(Bundle savedState) {
    super.onSaveInstanceState(savedState);
    savedState.putParcelableArrayList(LIST_STORAGE_KEY, new ArrayList<>(mItemAdapter.getModels()));
}

现在,我开始在我的应用程序错误报告中看到 TransactionTooLargeException .

Now I have started to see TransactionTooLargeException on my app error reporting.

在某些情况下,Im放入Bundle中的列表似乎太大(因为它是相当复杂的对象的集合).

It seems that in some cases the list which Im putting to Bundle, is too large (as it is collection of quite complex objects).

该如何处理?如何存储(和还原)我的Fragment状态.

How should I handle this case? How to store (and restore) my Fragment state.

是否可以在ViewPager中的片段上使用 setRetainInstance(true)?

Is it ok to use setRetainInstance(true) on Fragments inside ViewPager?

推荐答案

为保留大量数据,Google建议使用保留实例的Fragment进行处理.想法是创建没有所有必需字段的无视图的空片段,否则将其保存在Bundle中.添加setRetainInstance(true);片段的onCreate方法.然后将数据保存在Activity的onDestroy上的Fragment中并将其加载到onCreate上.这是活动"示例:

To preserve big chunks of data, Google is suggesting to do it with Fragment that retain instance. Idea is to create empty Fragment without view with all necessary fields, that would otherwise been saved in Bundle. Add setRetainInstance(true); to Fragment's onCreate method. And than save data in Fragment on Activity's onDestroy and load them onCreate. Here is and example of Activity:

public class MyActivity extends Activity {

private DataFragment dataFragment;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // find the retained fragment on activity restarts
    FragmentManager fm = getFragmentManager();
    dataFragment = (DataFragment) fm.findFragmentByTag("data");

    // create the fragment and data the first time
    if (dataFragment == null) {
        // add the fragment
        dataFragment = new DataFragment();
        fm.beginTransaction().add(dataFragment, "data").commit();
        // load the data from the web
        dataFragment.setData(loadMyData());
    }

    // the data is available in dataFragment.getData()
    ...
}

@Override
public void onDestroy() {
    super.onDestroy();
    // store the data in the fragment
    dataFragment.setData(collectMyLoadedData());
}
}

片段示例:

public class DataFragment 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;
}
}

这篇关于片段,在onSaveInstanceState上保存大量数据(如何防止TransactionTooLargeException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆