Remove all fragments from container

前端 未结 9 1601
死守一世寂寞
死守一世寂寞 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:32

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

提交回复
热议问题