Your problem is that you declared the google maps v2 fragment in XML, so every time you inflate the XML, it creates a new fragment. However, the fragment identifier is hardcoded in XML, so it will try to create a new fragment with the same identifier as the one that is already running. Two running fragments do not have the same identifier, so the application crashes.
The best way to fix this is to make a software fragment of the map. A good example of this can be found in ProgrammaticDemoActivity.java in the Google Maps sample project.
However, if for some reason you have to declare your fragment in XML, you can make it work by killing the old map fragment when you leave the view so that you can create a new one.
You can do something like:
private void killOldMap() { SupportMapFragment mapFragment = ((SupportMapFragment) getSherlockActivity() .getSupportFragmentManager().findFragmentById(R.id.map)); if(mapFragment != null) { FragmentManager fM = getFragmentManager(); fM.beginTransaction().remove(mapFragment).commit(); } }
and then call it from onDetach() and onDestroyView() to make sure the old map is killed.
As you can see, this is a kind of hack, and your best bet is to just create an application programmatically instead of using XML.
source share