Fragment, Save Large List Of Data On OnSaveInstanceState (how To Prevent TransactionTooLargeException)
Solution 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;
}
}
Solution 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)
).
Solution 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
Post a Comment for "Fragment, Save Large List Of Data On OnSaveInstanceState (how To Prevent TransactionTooLargeException)"