IllegalStateException Fragment already added - Android
Asked Answered
W

0

1

I am adding fragment like bellow :

@OnClick(R.id.lnNews)
void newsList() {
    String tagName = returnStatusFragment(getString(R.string.news_list_fragment));
    if (!TextUtils.isEmpty(tagName) && tagName.equals(getString(R.string.news_list_fragment))) {
        frameWorkTableFragment.setVisibility(View.VISIBLE);
        if (getActivity() != null) {
            android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            Fragment news = fragmentManager.findFragmentByTag(getString(R.string.news_list_fragment));
            getActivity().getSupportFragmentManager().beginTransaction()
                    .replace(R.id.frame_work_table_fragment,
                            news,
                            getString(R.string.news_list_fragment)).commit();
        }
        //Fragment is opened
    } else {
        if (getActivity() != null) {
            frameWorkTableFragment.setVisibility(View.VISIBLE);
            NewsListFragment newsListFragment = new NewsListFragment();
            getActivity().getSupportFragmentManager().beginTransaction()
                    .replace(R.id.frame_work_table_fragment,
                            newsListFragment,
                            getString(R.string.news_list_fragment))
                    .addToBackStack(getString(R.string.news_list_fragment))
                    .commit();
        }
    }
}

And in the Fragment I wrote doBackPress like bellow :

@Override
public void doBackTabZero(String nameFragment) {
    android.support.v4.app.FragmentManager fragmentManagers = getActivity().getSupportFragmentManager();
    if (fragmentManagers.getBackStackEntryCount() > 1) {
        fragmentManagers.popBackStackImmediate();
    } else {
        if (nameFragment.equals("SignsFragment")) {
            BusDisplayStatusFrameLayout busDisplayStatusFrameLayout = new BusDisplayStatusFrameLayout();
            busDisplayStatusFrameLayout.setDisplayStatusFrameLayout(1);
            EventBus.getDefault().post(busDisplayStatusFrameLayout);
            android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentManager.popBackStackImmediate();
            fragmentTransaction.remove(SignsFragment.this);
            fragmentTransaction.commit();
        }
    }
}

But get me crash often in this line :

fragmentManagers.popBackStackImmediate();

With this message :

java.lang.IllegalStateException: Fragment already added: NewsFragment{aed4cd1 #3 id=0x7f0a006e NewsFragment}

And in my activity my doBack is like bellow :

private Fragment getCurrentFragment() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    String fragmentTag = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount() - 1).getName();
    return fragmentManager.findFragmentByTag(fragmentTag);
}

Then :

@Override
public void onBackPressed() {
    int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
    if (backStackEntryCount > 0) {
        if (onBackPressedListenerTabWorkTable != null || onBackPressedListenerTabMou != null) {
            if (getCurrentFragment().getTag() != null && !TextUtils.isEmpty(getCurrentFragment().getTag())) {
                if (getCurrentFragment().getTag().equals("EventsFragment")
                        || getCurrentFragment().getTag().equals("WebViewListClassFragment")
                        || getCurrentFragment().getTag().equals("SignsFragment")
                        || getCurrentFragment().getTag().equals("NewsListFragment")) {
                    if (viewPagerMainActivity.getCurrentItem() != 0) {
                        viewPagerMainActivity.setCurrentItem(0);
                    }
                    onBackPressedListenerTabWorkTable.doBackTabZero(getCurrentFragment().getTag());
                } else if (getCurrentFragment().getTag().equals("SpecialNewsMouFragment")) {
                    if (viewPagerMainActivity.getCurrentItem() != 1) {
                        viewPagerMainActivity.setCurrentItem(1);
                    }
                    onBackPressedListenerTabMou.doBackTabOne(getCurrentFragment().getTag());
                }
            }
        }
    } else {
        super.onBackPressed();
        onBackPressedListenerTabWorkTable = null;
        onBackPressedListenerTabMou = null;
        finish();
    }
}
Womanhater answered 1/2, 2018 at 13:28 Comment(2)
Possible duplicate of IllegalStateException: Fragment already added in the tabhost fragmentRoxi
If you need to handle fragment back state then you need to handle back press on activity level instead of fragment.Shishko

© 2022 - 2024 — McMap. All rights reserved.