Fragment, save large list of data on onSaveInstanceState (how to prevent TransactionTooLargeException)

风格不统一 提交于 2021-02-18 05:22:14

问题


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.

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()));
}

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

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

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

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


回答1:


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());
}
}

And example of Fragment:

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;
}
}



回答2:


If you don't want your fragment to use setRetainInstance(true), then you can add an empty fragment with setRetainInstance(true) to your activity. This is useful since child fragments cannot use setRetainInstance(true).

Example:

public class BaseActivity extends Activity {

  RetainedFragment retainedFragment;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    retainedFragment = (RetainedFragment) getFragmentManager().findFragmentByTag("retained_fragment");
    if (retainedFragment == null) {
      retainedFragment = new RetainedFragment();
      getFragmentManager().beginTransaction().add(retainedFragment, "retained_fragment").commit();
    }
  }

  public <T> T getState(String key) {
    //noinspection unchecked
    return (T) retainedFragment.map.get(key);
  }

  public void saveState(String key, Object value) {
    retainedFragment.map.put(key, value);
  }

  public boolean has(String key) {
    return retainedFragment.map.containsKey(key);
  }

  public static class RetainedFragment extends Fragment {

    HashMap<String, Object> map = new HashMap<>();

    @Override public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setRetainInstance(true);
    }

  }

}

Then, in your fragment, you can cast getActivity() to your Activity class and use saveState(String, Object) and getState(String) to save your list.


There are other discussions on this which can be found at the following locations:

What to do on TransactionTooLargeException

android.os.TransactionTooLargeException on Nougat (Accepted answer suggests setRetainInstance(true)).




回答3:


setRetainInstance() is the best way to achieve that without side effects. Using static will cause memory leak and there is no use of saving the state in onSaveInstanceState() and getting it back since, setRetainInstance() does that for you.

So create a field for the list in fragment class and always check for null or size of list to begin operation of fetching latest data



来源:https://stackoverflow.com/questions/41953195/fragment-save-large-list-of-data-on-onsaveinstancestate-how-to-prevent-transac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!