java.lang.IllegalStateException: 片段已添加

java.lang.IllegalStateException: Fragment already added

我在更改屏幕方向时收到 IllegalStateException : Fragment already added。我有一个Activity和三个片段

代码:

private Fragment mFragment;
...
    @Override
protected void onCreate(Bundle savedInstanceState) {
initViews();
setBackStackBehavior();
mFragment = getSupportFragmentManager().findFragmentByTag(Constants.FIND_FRAGMENT_BY_TAG);

    if (mFragment == null) {
        mFragment = new SectionFragment();
    }
getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, mFragment, Constants.FIND_FRAGMENT_BY_TAG).commit();


private void setBackStackBehavior() {

        getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
                    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getSupportFragmentManager().popBackStack();
            getSupportFragmentManager().executePendingTransactions();
        }
    });
                } else {
                    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                    mDrawerToggle.syncState();
                    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mDrawerLayout.openDrawer(GravityCompat.START);
                        }
                    });
                }
            }
        });
    }

我也可以移动到第一个片段:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.section_selection:
                mFragment = new SectionFragment();

                getSupportFragmentManager().beginTransaction()
                        .setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left,
                                R.anim.enter_from_left, R.anim.exit_to_right)
                        .replace(R.id.container, mFragment, Constants.FIND_FRAGMENT_BY_TAG)
                        .commit();

                return true;
            case R.id.exit:
                finish();
            default:
                return super.onOptionsItemSelected(item);
        }
    }

我尝试创建 ArrayList<Fragment> mAdded 并在交易后在此处添加片段,然后在添加所有片段时:

if(mAdded.contains(mFragment.getClass())){
            getSupportFragmentManager().beginTransaction()
                    .show( mFragment).commit();
        }

没用。试图删除 onPause() 中的 Fragment,但它也没有解决我的问题。我尝试使用 commitAllowingStateLoss () 而不是 commit()

如何修复这个错误?

在 oncreate 中检查保存的实例状态是否为空,如果不是,请不要替换 fragment.Also 您可以使用按 ID 查找片段来检查您的片段是否已添加并可用。

if(savedInstanceState == null){
   getSupportFragmentManager().beginTransaction()
        .replace(R.id.container, 
                  mFragment,Constants.FIND_FRAGMENT_BY_TAG).commit();
}

    if(((Fragment) getSupportFragmentManager().findFragmentByTag(Constants.FIND_FRAGMENT_BY_TAG)) == null){

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, 
                      mFragment,Constants.FIND_FRAGMENT_BY_TAG).commit();
}