底部导航图标不变



我使用底部导航栏。一切都很完美,但当我打开一个新的片段,并使用后退按钮销毁它时,我的应用程序回到了基础片段,但底部导航栏选中的项目没有改变。它保持上次点击的位置。让我用一些图片来解释

图像1 ++图像2 ++图像3

正如你所看到的,当底部导航栏的选定项目是配置文件项目时,我正在打开片段。然后我使用后退键并回到主片段,但底部导航栏的选定项目仍然是配置文件项目。

主机片段:函数是在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

试试:

binding.bottomNavigationView.setSelectedItemId(R.id.nav_mainpage)

最新更新