【发布时间】:2021-12-04 14:47:12
【问题描述】:
我正在使用底部导航栏。一切正常,但是当我打开一个新片段并使用后按按钮将其销毁时,我的应用程序将返回基本片段,但底部导航栏选定的项目不会改变。它保持最后点击的位置。让我用一些图片解释一下
Image 1 +++ Image 2 +++ Image 3
如您所见,当底部导航栏的选定项目是配置文件项目时,我正在打开片段。然后我使用后按并返回主片段,但底部导航栏的选定项目仍然是配置文件项目。
Host Fragment:该函数在 onViewcreated 内部(我正在使用 replacefragment 函数为第一次打开设置基本片段)
binding.bottomNavigationView.setOnItemSelectedListener {
when(it.itemId){
R.id.nav_profile -> {replaceFragment(ProfileFragment())
}
R.id.nav_mainpage -> {replaceFragment(MainPageFragment())
}
}
return@setOnItemSelectedListener true
}
replaceFragment(MainPageFragment())
private fun replaceFragment(fragment: Fragment){
val transaction = activity?.supportFragmentManager?.beginTransaction()
transaction?.replace(R.id.frameLayout,fragment)
transaction?.commit()
}
我尝试了什么:我尝试了这种方法及其变体,但它们都不起作用
binding.bottomNavigationView.selectedItemId = R.id.nav_mainpage
【问题讨论】:
标签: android android-fragments navigation