MapFragment in Fragment, alternatives?

android, fragment, google-maps-android-api-2, mapfragment

Solution

Use MapView instead of MapFragment in your Fragment's layout. Remember to call MapView's lifecycle methods:

- onCreate(Bundle)

- onResume()

- onPause()

- onDestroy()

- onSaveInstanceState(Bundle)

- onLowMemory()

as described here.

Btw. you shouldn't be using MapFragment, only SupportMapFragment and support library.

Edit:

If you switch to support library, you can use code from comment #1 here: http://code.google.com/p/gmaps-api-issues/issues/detail?id=5064#c1

Problem

I need your help... I work on it until 3 days. My app is working with fragments. One of these fragments has to display a map from the Google Maps V2 api for Android. Currently, I'm using a MapFragment, but no surprise, a fragment in a fragment is not a good idea, but it works, the map is displaying, i can edit it but when I switch of main fragment and return on it. Caused by: java.lang.IllegalArgumentException: Binary XML file line #59: Duplicate id 0x7f070041, tag null, or parent id 0x7f070040 with another fragment for com.google.android.gms.maps.MapFragment at android.app.Activity.onCreateView(Activity.java:4252) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673) This is the cause when I go on another fragment and return to the one which contains the map. I'm searching until 3 days to fix this but no great results. To resume for you, I've an Activity which calls a fragment which contains a MapFragment in the layout file. If you need more, just ask :) Thanks Edit : Here is the code to change Fragment in the main Activity ``` private void swtichFragment(Fragment fragment, Bundle bundle) { fragment.setBundle(this, bundle); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.rightFragmentPlaceHolder, fragment); fragmentTransaction.commit(); mRightFragment = fragment; } ```

Original source