Waza_Be
Waza_Be

Reputation: 39558

FragmentTransaction.remove has no effect

My requirement is quite simple: I have a button that should replace a FragmentA by FragmentB.

This sounds easy and nearly work, the big problem is that the old fragment is not removed and the new placed on the front of the old one and they are "living" together in my layout.

enter image description here

The Code:

FragmentManager fragMgr = a.getSupportFragmentManager();
Fragment currentFragment = (Fragment) fragMgr.findFragmentById(R.id.fragmentitself);

if(currentFragment!=null){

     FragmentTransaction fragTrans = fragMgr.beginTransaction();
     fragTrans.remove(currentFragment);

     FragmentB newFragment = new FragmentB();
     fragTrans.replace(R.id.fragmentcontainer, newFragment);
     // I have also tried with R.id.fragmentitself
     fragTrans.addToBackStack(null);
     fragTrans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
     fragTrans.commit();
}

The Layout:

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2" 
    android:id="@+id/fragmentcontainer">

    <fragment
        android:id="@+id/fragmentitself"
        android:name="com.WazaBe.MyApp.FragmentA"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

Upvotes: 9

Views: 10325

Answers (4)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

Solution

First, you have to remove your fragment from XML and just keep empty container there:

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2" 
    android:id="@+id/fragmentcontainer" />

Then you need to add your com.WazaBe.MyApp.FragmentA fragment from code, i.e. in onCreate() of your parent Activity.

Explanation

It is because your transactions manipulate content of ViewGroup such as said FrameLayouts. The catch is, that you can only manipulate elements you added from code as whatever is inflated from XML layout is considered "read-only". So when you put your Fragment directly into your XML layout, then it becomes permanent part of the view hierarchy and because it is permanent and the whole hierarchy is "read-only" it cannot be removed from code.

Once you get your layout fixed and Fragment extracted, the remove() call is no longer needed - it will suffice to just do replace().

Upvotes: 16

Hitesh Sahu
Hitesh Sahu

Reputation: 45100

Same Answer In Kotlin

   private fun showDetailView(position: Int) {
            val fragTrans: FragmentTransaction = supportFragmentManager.beginTransaction()
            val fragment: Fragment
            when (position) {
                0 -> fragment = FragmentA()
                1 -> fragment = FragmentB()
                else ->
                    fragment = FragmentC()
            }
            fragTrans.replace(
                    R.id.itemDetailContainer,
                    fragment).commit()
            fragTrans.addToBackStack(null);
            fragTrans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        }
    
    }

Upvotes: 0

Tan Hoo Chuan Nickson
Tan Hoo Chuan Nickson

Reputation: 1162

if we use Fragment container on activity, it require select one default FragmentABC, the default FragmentABC view cannot removed even we use the ft.remove.

Use FrameLayout container instead of Fragment container if you want a empty view of fragment on activity startup.

Upvotes: 0

litang0908
litang0908

Reputation: 103

If you want to place a Fragment in a View Container(Such as a Framelayout),you must make sure that your container is empty(only this you can put a fragment into it).you cannot replace a fragment writed in XML file,you should add A into the container by JAVA code ,and when you don't neet id ,you can replace it by B;

at first ,your container is empyt:

<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" 
android:id="@+id/fragmentcontainer">
</FrameLayout>

OK,you put FragmentA into it:

 FragmentTransaction fragTrans = fragMgr.beginTransaction();
 fragTrans.remove(currentFragment);
 FragmentA fragA= new FragmentA();
 fragTrans.add(R.id.fragmentcontainer, fragA).commit();

NOW,if you want to replace:

FragmentTransaction fragTrans = fragMgr.beginTransaction();
 FragmentB newFragment = new FragmentB();
 fragTrans.replace(R.id.fragmentcontainer, newFragment);
 // I have also tried with R.id.fragmentitself
 fragTrans.addToBackStack(null);
 fragTrans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
 fragTrans.commit();

Upvotes: 7

Related Questions