Is there another way of saving the state of the nested fragment ? Or if we shouldn\'t do this, why ? Thanks !
02-13 11:42:43.258: E/AndroidRuntime(7167): ja
Problem: mChildFrgamentManager
is being recreated (https://code.google.com/p/android/issues/detail?id=74222)
Workaround: Retaining mChildFrgamentManager
if fragment has setRetainInstance(true)
:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (getRetainInstance()) {
if (mRetainedChildFragmentManager != null) {
try {
Field childFMField = Fragment.class.getDeclaredField("mChildFragmentManager");
childFMField.setAccessible(true);
childFMField.set(this, mRetainedChildFragmentManager);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
mRetainedChildFragmentManager = getChildFragmentManager();
}
}
}
Warning: With this code, setRetainInstace
should be called before onAttach
.
P.S: This is a bit improved version of @attels answer.