Onuray Sahin
Onuray Sahin

Reputation: 4135

Not able to inflate Google Maps API v2 in fragment twice

Ok, I have already checked out about 100 SO QA.

Here is the story: I have a SlidingMenu which has lets say "Create Event" and "All Events" items. "Create Event" menu has a Maps Fragment which is as below. User selects "Create Event" and maps is on screen with no error, than user selects "All Events" and events are shown and user selects "Create Events" again and boom:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #45: Duplicate id 0x7f040058, tag null, or parent id 0x0 with another fragment for pl.mg6.android.maps.extensions.SupportMapFragment

CreateEventFragment is created everytime when user selects from left menu:

public void showCreateNewEventFragment() {
    CreateEventFragment fragment = new CreateEventFragment();
    showFragment(fragment);
    slidingMenu.showContent();
}

create_event.xml

...    
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/searchLayout"
    android:layout_marginTop="8dp"
    android:background="@color/app_color"
    android:padding="6dp" >

    <fragment
        android:id="@+id/map"
        android:name="pl.mg6.android.maps.extensions.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
...

CreateEventFragment.java

public class CreateEventFragment extends BaseFragment {

    private GoogleMap mMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    setRetainInstance(true);
    View v = (View) inflater.inflate(R.layout.create_event, container,
            false);
    ((MainActivity) getActivity())
            .showSlidingMenu(LeftNavigation.ITEM_CREATE_NEW_EVENT);
    initGoogleMaps(v, savedInstanceState);
    return v;
}

private void initGoogleMaps(View v, Bundle savedInstanceState) {
    EventshipApplication app = (EventshipApplication) getActivity()
            .getApplication();
        SupportMapFragment mMapFragment = ((SupportMapFragment) getActivity()
                .getSupportFragmentManager().findFragmentById(R.id.map));
        mMap = mMapFragment.getExtendedMap();
        mMap.getUiSettings().setMyLocationButtonEnabled(true);
}
}

So, I want to be able to create / use MapFragment more than twice. Is there any suggestions / workarounds?

EDIT:

public void showFragment(Fragment fragment) {
    FragmentTransaction transaction = getSupportFragmentManager()
            .beginTransaction().add(android.R.id.content, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

Upvotes: 2

Views: 564

Answers (2)

Onuray Sahin
Onuray Sahin

Reputation: 4135

Here is the solution:

v = app.getMapLayout();
    if (v == null) {
        v = (View) inflater.inflate(R.layout.create_event, container, false);
        app.setMapLayout(v);
    } else {
        ViewGroup parent = (ViewGroup) v.getParent();
        parent.removeView(v);
    }

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007322

Use replace() instead of add(), as you already have a fragment in android.R.id.content the second time that you call showFragment().

You may also need to pop the previous transaction off the back stack, or get rid of the addToBackStack() call.

Upvotes: 1

Related Questions