0

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)
}
Victor
  • 331
  • 2
  • 7
  • Does this answer your question? [How to handle bottom navigation perfectly with back pressed](https://stackoverflow.com/questions/43870485/how-to-handle-bottom-navigation-perfectly-with-back-pressed) – Nitish Oct 22 '21 at 13:23
  • @Nitish , no :( – Victor Oct 22 '21 at 13:35
  • What's the issue , please update your code with backpress implemented – Nitish Oct 22 '21 at 14:31

0 Answers0