Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
FIX: Nested fragments not retained in latest support library releases
public class BaseFragment extends Fragment {
private FragmentManager mRetainedChildFragmentManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
FragmentManager childFragmentManager = getChildFragmentManager();
mRetainedChildFragmentManager = childFragmentManager;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
FragmentManager childFragmentManager = getChildFragmentManager();
//Use reflection to set the child fragment manager for restoring nested fragments
//This is a bug in support libraries v20/21
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();
}
}
}
}
@eriktx11

This comment has been minimized.

Copy link

@eriktx11 eriktx11 commented Aug 24, 2016

Hello Thomas,
Sorry to bother man, but I'm so frustrated unable to have my google map working. I'm stuck with the problem for more that 4 days. I don't know for how much longer this will continue.
I'm crossing finger that you may be able to clone my repository and show me the code that would solve my problem. I'm implementing your code and it still gives me 'not view found' error.
I have posted this problem in stackoverflow but I don't have more answers. Could you please help me to solve the issue.?
My google map crashes in landscape mode. This is my repo. https://github.com/eriktx11/joshua

I'll be eternally thankful for this.

Erik..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment