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 に答える