I have an application with three tabs.
Each tab has its own layout .xml file. The main.xml has its own map fragment. It\'s the one that shows up when the application
I had the same issue and was able to resolve it by manually removing the MapFragment
in the onDestroy()
method of the Fragment
class. Here is code that works and references the MapFragment
by ID in the XML:
@Override
public void onDestroyView() {
super.onDestroyView();
MapFragment f = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
If you don't remove the MapFragment
manually, it will hang around so that it doesn't cost a lot of resources to recreate/show the map view again. It seems that keeping the underlying MapView
is great for switching back and forth between tabs, but when used in fragments this behavior causes a duplicate MapView
to be created upon each new MapFragment
with the same ID. The solution is to manually remove the MapFragment
and thus recreate the underlying map each time the fragment is inflated.
I also noted this in another answer [1].