删除所有(子)碎片的正确方法 [英] Correct way to remove all (Child) fragments

查看:81
本文介绍了删除所有(子)碎片的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一堆子片段动态地加载到父片段的linearlinearLayout(fragmentContainer)中,然后当用户单击按钮时,我需要将其全部删除并添加新的片段。我不知道每次都会添加的确切片段数。这是我一次删除所有碎片的方法

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

现在,我可以使用片段事务方法添加新的了。
这种删除所有片段的方法超级简单,并且比使用remove()逐个删除每个片段对我更有效,但这是一个好习惯吗?性能如何?您是否建议一种更好的方法?

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?

推荐答案


这是我一次删除所有碎片的方式

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.

它确实删除了任何碎片。它删除视图。这就是为什么将该方法命名为 removeAllViews()的原因。

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您建议使用更好的方法?

Do you recommend a better way?

跟踪未完成的片段(例如,使用 ArrayList< Fragment> ),然后遍历该列表,将每个参数传递给 FragmentTransaction remove()方法c>。

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.

这篇关于删除所有(子)碎片的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆