Error Opening Supportmapfragment for Second Time

Error opening SupportMapFragment for second time

Instead of declaring de SupportMapFragment in layout, do it programatically and be sure you use getChildFragmentMananger instead of the classic getFragmentManager() to create the fragment.

  mMapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction =
mMapFragment.getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.map_root, mMapFragment);
fragmentTransaction.commit();

Keep this SupportMapFragment mMapFragment as you will need it to retrieve the GoogleMap object:

  GoogleMap map = mMapFragment.getMap();

SherlockFragment which contains SupportMapFragment does not open second time

You are incorrectly adding SupportMapFragment into your fragment.

Please read about nested fragments.

When nesting fragments remember these things:

  • use getChildFragmentManager,
  • don't add SupportMapFragment in xml.

SupportMapFragment show for second time

There is a similar known issue, look here

This is a bug in the nested fragments. Basically, the child FragmentManager ends up with a broken internal state when it is detached from the activity.

A short-term workaround is to add the following code in your fragment.

@Override
public void onDetach() {
super.onDetach();

try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}


Related Topics



Leave a reply



Submit