IllegalStateException: Fragment already added in the tabhost fragment

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 05:37:53

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

You just have to check one condition in your fragment mentioned below:

if(!isAdded())
{
    return;
}

isAdded = Return true if the fragment is currently added to its activity. Taken from the official document. This will not add that fragment if it is already added

Check below link for a reference:
http://developer.android.com/reference/android/app/Fragment.html#isAdded()

You just have to check one condition before start fragment transaction

 if (!fragmentOne.isAdded()){
            transaction = manager.beginTransaction();
            transaction.add(R.id.group,fragmentOne,"Fragment_One");
            transaction.commit();
 }

this is working perfactly for me...

Remove the old fragment in case it is still added and then add the new fragment:

FragmentManager fm = getSupportFragmentManager();
Fragment oldFragment = fm.findFragmentByTag("fragment_tag");
if (oldFragment != null) {
    fm.beginTransaction().remove(oldFragment).commit();
}
MyFragment newFragment = new MyFragment();
fm.beginTransaction().add(newFragment , "fragment_tag");

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.

Farid Haq

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

It even can occur if in FragmentStatePagerAdapter of your ViewPager you create an item that already exists:

override fun getItem(position: Int): Fragment {
    return tabs[0] // Right variant: tabs[position]
}

(private val tabs: List<Fragment> is a list of fragments in tabs).

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.

Add Fragment as below

FragmentTransaction t = getSupportFragmentManager().beginTransaction();
    t.replace(R.id.realtabcontent, mFrag);
    t.addToBackStack(null);
    t.commitNowAllowingStateLoss();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!