Remove all fragments from container

2020-02-17 10:02发布

Is there a way to remove all fragments which already added the specific view with its view id?

For example I want to remove all fragments which is added R.id.fragmentcontainer view.

8条回答
Deceive 欺骗
2楼-- · 2020-02-17 10:05

Use this code

activity?.let {
it.supportFragmentManager.fragments.forEach { fragment ->
        it.supportFragmentManager.beginTransaction().remove(fragment).commit()
    }
}

Hope it helps.

Thank you.

查看更多
\"骚年 ilove
3楼-- · 2020-02-17 10:13

In case someone is looking for a code in Kotlin:

private fun clearFragmentsFromContainer() {
            val fragments = supportFragmentManager.fragments
            if (fragments != null) {
                for (fragment in fragments) {
                    supportFragmentManager.beginTransaction().remove(fragment).commit()
                }
            }
            //Remove all the previous fragments in back stack
            supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
}
查看更多
乱世女痞
4楼-- · 2020-02-17 10:14

Try this, hope it helps :D

try {
if(manager.getFragments()!=null){
    if(manager.getBackStackEntryCount()>0) {
        for (int i = 0; i < manager.getBackStackEntryCount(); i++)
            manager.popBackStack();

        manager.beginTransaction().remove(getSupportFragmentManager()
        .findFragmentById(R.id.main_content))
        .commit();
        }
    }
}catch (Exception e){
    e.printStackTrace();
} 
查看更多
不美不萌又怎样
5楼-- · 2020-02-17 10:16

You can try below code

getSupportFragmentManager().beginTransaction().remove(frag).commit(); 

*frag is the object of fragment that you want to remove.

 OR
getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentById(R.id.your_container)).commit();

it will remove the fragment that is loded in "your_container" container.

HapPy coding.

查看更多
霸刀☆藐视天下
6楼-- · 2020-02-17 10:17

Save all your fragments in an ArrayList.

Initializing:

List<Fragment> activeCenterFragments = new ArrayList<Fragment>();

Adding fragment to list:

private void addCenterFragments(Fragment fragment) {
    fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.empty_center_layout, fragment);
    activeCenterFragments.add(fragment);
    fragmentTransaction.commit();
}

When you want to remove all them, do the following:

private void removeActiveCenterFragments() {
    if (activeCenterFragments.size() > 0) {
        fragmentTransaction = fragmentManager.beginTransaction();
        for (Fragment activeFragment : activeCenterFragments) {
            fragmentTransaction.remove(activeFragment);
        }
        activeCenterFragments.clear();
        fragmentTransaction.commit();
    }
}

I have used this method in production for years, and it works like a charm. Let me know if you have any questions.

查看更多
等我变得足够好
7楼-- · 2020-02-17 10:23

More optimized version
There is no need in multiple call of commit so lets call it one time at the end

supportFragmentManager.fragments.let {
    if (it.isNotEmpty()) {
        supportFragmentManager.beginTransaction().apply {
            for (fragment in it) {
                remove(fragment)
            }
            commit()
        }
    }
}
查看更多
登录 后发表回答