[英]Bottom Navigation Icon Doesn't Change
我正在使用底部導航欄。 一切正常,但是當我打開一個新片段並使用后退按鈕銷毀它時,我的應用程序將返回基礎片段,但底部導航欄選定的項目不會改變。 它保持最后點擊的位置。 讓我用一些圖片來解釋
正如您所看到的,當底部導航欄的所選項目是個人資料項目時,我正在打開片段。 然后我使用后按並返回主片段,但底部導航欄的選定項目仍然是配置文件項目。
主機片段:該函數在 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.