AggieDev
AggieDev

Reputation: 5045

Getting error: fragment already added?

Why is this returning the "fragment already added" error and crashing the app?

android.support.v4.app.FragmentManager fm = getSupportFragmentManager();

    android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();



    ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
    ft.replace(R.id.fragment_container, tempmainfrag);
    ft.commit();
    for(int i = 0; i < fm.getBackStackEntryCount(); i++)
    {
        fm.popBackStack();
    }

I'm guessing it is because of the for loop as it works without it, but I am needing to clear the back stack, how can I properly do this?

Upvotes: 1

Views: 2883

Answers (1)

Analizer
Analizer

Reputation: 1444

I've had the same error when working with mapfragments, this is what finally solved it for me:

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

    SupportMapFragment f = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.current_fragment);
    if (f != null)
        getFragmentManager().beginTransaction().remove(f).commit();
}

Hope it helps.

Upvotes: 1

Related Questions