简体   繁体   中英

Android IllegalStateException Fragment already added

I'm getting an exception with my android application. It looks like when the backbutton is pressed and we want to pop the back stack, (I think that is what the stack trace is informing me) an illegal state exception is thrown.

AppVersion: 5.0.3
DeviceType: phone
DeviceDetails: SM-J510FN
SystemVersion: 6.0.1
Details: Fragment already added: CategoriesFragmentMobileImpl
{a6aafc9 #0 id=0x7f1200e0 boom.android.new_app.NewCategories.CategoriesFragment}
StackTrace: 

java.lang.IllegalStateException: Fragment already added: CategoriesFragmentMobileImpl{a6aafc9 #0 id=0x7f1200e0 boom.android.new_app.NewCategories.CategoriesFragment}
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1679)
at android.support.v4.app.BackStackRecord.executePopOps(BackStackRecord.java:777)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2353)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
–

I'm using the support library

compile 'com.android.support:appcompat-v7:25.1.1'

Here is the only place in my code where that fragment is added:

 @Override
    protected void bindViews() {

        Fragment fragment = getSupportFragmentManager().findFragmentByTag(CategoriesFragment.TAG);
        if(fragment == null)
            fragment = CategoriesFragmentMobileImpl.newInstance(this);

        if(fragment.isAdded() == false) {
            getSupportFragmentManager().beginTransaction().
                    replace(R.id.main_fragment_container, fragment, CategoriesFragment.TAG)
                    .commit();
        }

        horizontalScrollView = (HorizontalScrollView)findViewById(R.id.breadcrumbs_ScrollView);
        breadcrumbsContainer = (LinearLayout) findViewById(R.id.breadcrumbs_container);
        second_toolbar = findViewById(R.id.second_toolbar);
        bindNavigationMenu(R.id.drawer_layout, navigationView, null);

    }

I was looking at some of the bugs in Android and could this issue be possibly related to: https://issuetracker.google.com/issues/36928785

Here is the code for onBackPressed

@Override
public void onBackPressed() {

    if(breadcrumbsContainer.getChildCount() > 0)
    {
    removeBreadCrumb(-1);
}
if(getSupportFragmentManager().getBackStackEntryCount() > 0)
{        
    getSupportFragmentManager().popBackStack();
}
else
{
    super.onBackPressed();
}

}

Any idea how to replicate this issue or even what's going on with the stack trace so I can have a better understanding?

尝试使用replaceFragment()代替addFragment()!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM