Fragment already added IllegalStateException

前端 未结 11 1431
一向
一向 2020-11-28 07:42

I use this method on my container Activity to show a BFrag

public void showBFrag()
{
    // Start a new FragmentTransaction
    FragmentTransaction fragmentT         


        
相关标签:
11条回答
  • 2020-11-28 08:01

    try this after fragmentTransection.replace()

    fragmentTransection.addToBackStack(null);
    fragmentTransection.commitAllowingStateLoss();
    
    0 讨论(0)
  • 2020-11-28 08:04

    If the state of the activity has already been saved its no longer safe to call commit. You must call commitAllowingStateLoss() instead. Hope this helps!

    Edit: ok I've taken a closer look at your issue, problem is you are trying to add a fragment that has already been added. Even if you use replace or remove calls you can't do this. Only work around I have found is to create a new instance of a fragment and add it every time. Once you remove or replace a fragment it is best to drop all of your references to it so the GC can take care of it.

    0 讨论(0)
  • 2020-11-28 08:06

    Probably not related to this issue directly, but I've also noticed that setting a transition to the FragmentTransaction will cause an IllegalStateException, while not setting a transition will not.

    Here's the bug for this issue: http://code.google.com/p/android/issues/detail?id=25598

    0 讨论(0)
  • 2020-11-28 08:06

    Check whether the frament is already added or not using the method fragment.isAdded() Do replace or add the fragment accordingly

    0 讨论(0)
  • 2020-11-28 08:08

    if(mFragment.isAdded()) { return; //or return false/true, based on where you are calling from }

    0 讨论(0)
提交回复
热议问题