IllegalStateException: Fragment already added in the tabhost fragment

后端 未结 11 1140
暗喜
暗喜 2020-12-04 14:07
FATAL EXCEPTION: main
Process: com.example.loan, PID: 24169
java.lang.IllegalStateException: Fragment already added: FormFragment{428f10c8 #1 id=0x7f050055 form}
            


        
相关标签:
11条回答
  • 2020-12-04 14:41

    Add Fragment as below

    FragmentTransaction t = getSupportFragmentManager().beginTransaction();
        t.replace(R.id.realtabcontent, mFrag);
        t.addToBackStack(null);
        t.commitNowAllowingStateLoss();
    
    0 讨论(0)
  • 2020-12-04 14:44

    To my surprise, I made stupid mistake by calling the fragment transaction twice:

    if (!FirebaseManager.isClientA && !FirebaseManager.isClientB) {
          fragment = new FragmentA();
          getFragmentManager().beginTransaction().add(R.id.fragment_frame, fragment, null).addToBackStack("").commit();
    } else if (FirebaseManager.isClientB) {
          fragment = new FragmentB();
    } else {
          fragment = new FragmentC();
    }
    getFragmentManager().beginTransaction().add(R.id.fragment_frame, fragment, null).addToBackStack("").commit();
    

    Make sure you don't make the same mistake.

    0 讨论(0)
  • 2020-12-04 14:48

    Sometimes it happens for not finding proper id from the respective layout. I faced this problem. Then after many hours I found that I set wrong recyclerview id. I change it, and works fine for me.

    So, double check your fragment layout.

    0 讨论(0)
  • 2020-12-04 14:55

    This happens when we try to add same fragment or DialogFragment twice before dismissing,

    just call

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

    Having said that, I don't see any reason why to remove old fragment and add the same fragment again since we can update the UI/data by simply passing parameters to the method inside the fragment

    0 讨论(0)
  • 2020-12-04 14:56

    For me it works like:

    Fragment oldFragment = manager.findFragmentByTag(READER_VIEW_POPUP);
    if (oldFragment != null) {
        manager.beginTransaction().remove(oldFragment).commit();
    }
    
    FragmentTransaction ft = manager.beginTransaction();
    ft.add(this, tag);
    ft.commit();
    
    0 讨论(0)
  • 2020-12-04 14:57

    You can try this:

     if (dialogFolderGallery.isAdded()) {
                    dialogFolderGallery.dismiss();
                } else { //bla...bla..
    }
    
    0 讨论(0)
提交回复
热议问题