java.lang.IllegalStateException:已添加片段

时间:2016-02-24 01:56:41

标签: android android-fragments transactions screen-orientation

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

CODE:

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并在事务后添加Fragment,然后在添加所有片段时添加:

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

没有帮助。试图删除onPause()中的Fragment,但它也没有解决我的问题。我尝试使用commitAllowingStateLoss ()代替commit()

如何修复此错误?

1 个答案:

答案 0 :(得分:0)

在oncreate中检查保存的实例状态是否为null,如果不是不替换片段。也可以通过id使用find fragment检查您的片段是否已经添加和可用。

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

}