Skip to content

Instantly share code, notes, and snippets.

@clowestab
Created February 22, 2015 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clowestab/ea8eaa99c5c4153b2346 to your computer and use it in GitHub Desktop.
Save clowestab/ea8eaa99c5c4153b2346 to your computer and use it in GitHub Desktop.
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
Copy link

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