Skip to content Skip to sidebar Skip to footer

Why Doesn't Detach() Work On Fragmenttransaction And Remove() Does?

I'm working on a TabHost whose tabs have associated a Fragment (each tab a different one). Each of those Fragments have inside an instance of another Fragment which is a login bar

Solution 1:

You cannot remove an instance of a Fragment which has been declared in your layout XML file (by design)

When you use FragmentTransactions, you're manipulating ViewGroups containing the layout of a Fragment.

When you declare your Fragment/Class in a layout, it's part of the View hierarchy, so you can't remove it.

You can, however, add a New Fragment into that layout, because it will act as a container.

Try changing your Layout from:

    <fragment 
      android:id="@+id/firsttab_loginrow"class="com.mydomain.myproject.LoginRowFragment"
      android:tag="1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />

to:

    <FrameLayout 
      android:id="@+id/firsttab_loginrow"
      android:tag="1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />

And create a FragmentTransaction to add/replace/show/hide according to what you want to do.

Post a Comment for "Why Doesn't Detach() Work On Fragmenttransaction And Remove() Does?"