ThanosFisherman
ThanosFisherman

Reputation: 5859

Correct way to remove all (Child) fragments

I load a bunch of Child fragments dynamically inside a Parent's Fragment linearLayout (fragmentContainer), then when user clicks a button, I need to remove them all and add new ones. I don't know the exact number of fragments that will be added each time. This is my way of removing all the fragments at once

LinearLayout ll = (LinearLayout) view.findViewById(R.id.fragmentContainer);
ll.removeAllViews();

Now I can add the new ones using fragment transaction methods. This way of removing all fragments is super easy and works for me better than removing each fragment one by one using remove() But is it a good practice? How about performance? Do you recommend a better way?

Child fragments

Upvotes: 4

Views: 12616

Answers (4)

Cristofer
Cristofer

Reputation: 1106

I would like to suggest you: Use Kotlin and fragment-ktx using this one:

In your gradle:

implementation "androidx.fragment:fragment-ktx:$androidXFragmentKtxVersion"

In your Fragment

childFragmentManager
      .findFragmentByTag(YOUR_TAG)?.let { fragment ->
        childFragmentManager.commit(allowStateLoss = true) {
          remove(fragment)
        }
      }

If you are not using childFragmentManager use requireActivity().supportFragmentManager instead

requireActivity().supportFragmentManage
          .findFragmentByTag(YOUR_TAG)?.let { fragment ->
            requireActivity().supportFragmentManage.commit(allowStateLoss = true) {
              remove(fragment)
            }
      }

Upvotes: 0

MatPag
MatPag

Reputation: 44851

In Kotlin you can do:

repeat(fragmentManager.backStackEntryCount) {
    fragmentManager.popBackStack()
}

Where fragmentManager could be one of:

  • Activity.supportFragmentManager
  • Fragment.requireActivity().supportFragmentManager
  • Fragment.childFragmentManager
  • Fragment.parentFragmentManager which replaced requireFragmentManager()

Upvotes: 1

The Original Android
The Original Android

Reputation: 6215

If you simply want to remove all fragments, code below:

FragmentManager fm = getActivity().getSupportFragmentManager();
for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {    
    fm.popBackStack();
}

This code is good only if you use the add method of FragmentTransaction when referencing fragments. Method popBackStack is used for removing.

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1007099

This is my way of removing all the fragments at once

No, it isn't. It is your way of removing all views from that container.

This way of removing all fragments is super easy and works for me.

It does remove any fragments. It removes views. That is why the method is named removeAllViews().

But is it a good practice?

No. For starters, when you rotate your device or undergo a configuration change, you will notice that all your "removed" fragments come back.

Do you recommend a better way?

Keep track of the outstanding fragments (e.g., using an ArrayList<Fragment>), then iterate over that list, passing each to a remove() method on a FragmentTransaction.

Upvotes: 4

Related Questions