When I press the back button, it opens the previous fragment. But in the bottom navigation, another tab remains highlighted.
How do I make the tab on the bottom navigation change when the fragment changes too?
private lateinit var mainBottomBar: BottomNavigationView
private val navigationBottomViewListener = NavigationBarView.OnItemSelectedListener {
val fragment = when (it.itemId) {
R.id.homeMenu -> HomeFragment()
R.id.pomodoroMenu -> PomodoroFragment()
R.id.statisticsMenu -> StatisticsFragment()
else -> return@OnItemSelectedListener false
}
replaceFragmentWithAddBackStack(fragment)
return@OnItemSelectedListener true
}
private fun replaceFragmentWithAddBackStack(fragment: Fragment) {
supportFragmentManager.beginTransaction().addToBackStack(null).replace(R.id.mainContainer, fragment).commit()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mainBottomBar = findViewById(R.id.mainBottomBar)
mainBottomBar.setOnItemSelectedListener(navigationBottomViewListener)
}