IllegalStateException: Fragment already added in the tabhost fragment

后端 未结 11 1141
暗喜
暗喜 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 15:00

    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");
    
    0 讨论(0)
  • 2020-12-04 15:00

    I have this error when not wrapping my body XML inside a ViewGroup inside FrameLayout.

    Error:

    <FrameLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".screens.home.HomeEpoxyFragment">
    
        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/swipe_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv"
                android:overScrollMode="never"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    
    </FrameLayout>
    

    Solved:

    <FrameLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".screens.home.HomeEpoxyFragment">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
                android:id="@+id/swipe_refresh_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rv"
                    android:overScrollMode="never"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
            </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </FrameLayout>
    

    Hope this may help someone.

    0 讨论(0)
  • 2020-12-04 15:00

    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).

    0 讨论(0)
  • 2020-12-04 15:03

    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...

    0 讨论(0)
  • 2020-12-04 15:04

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

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