1

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?

4

1 回答 1

1

我在使用 mapfragments 时遇到了同样的错误,这就是最终为我解决的问题:

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

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

希望能帮助到你。

于 2013-03-28T07:35:33.690 回答