Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 737
devha Avatar asked Jan 31 '17 08:01

devha


2 Answers

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;
}
}
like image 155
Shobhit Avatar answered Sep 20 '22 14:09

Shobhit


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)).

like image 25
Jared Rummler Avatar answered Sep 22 '22 14:09

Jared Rummler