IllegalStateException Fragment already added - Android

做~自己de王妃 提交于 2019-11-27 08:38:30

问题


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();
    }
}

来源:https://stackoverflow.com/questions/48563909/illegalstateexception-fragment-already-added-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!