Remove all fragments from container

前端 未结 9 1574
死守一世寂寞
死守一世寂寞 2021-02-01 00:56

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.fragmentcon

9条回答
  •  礼貌的吻别
    2021-02-01 01:31

    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)
    }
    

提交回复
热议问题