底部导航图标不变

Bottom Navigation Icon Doesn't Change

我正在使用底部导航栏。一切正常,但是当我打开一个新片段并使用后退按钮销毁它时,我的应用程序将返回基本片段,但底部导航栏所选项目不会改变。它保持最后点击的位置。放几张图解释一下

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

}

我 tried:I 尝试了此方法及其变体,但其中 none 有效

binding.bottomNavigationView.selectedItemId = R.id.nav_mainpage

试试这个:

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