Uncheck bottomnavigationmenu item when navigating to other fragment

Viewed 29

I have a BottomNavigationMenu in my MainActivity where I can navigate between 4 fragments. By default, the first fragment that the user can see, is the first item and the item must be checked. What I want to do, is when the user navigate to a fragment which is not in the BottomNavigationMenu, uncheck the items in BottomNavigationMenu. How can this be reached? To navigate between fragments, I'm using navigation graph.

In MainActivity:

  private fun setUpBottomNavigationMenu() {
    binding.navigationmenu.apply {

        val navigationHostFragemt = supportFragmentManager.findFragmentById(R.id.containerview)
        val navigationController = navigationHostFragment.navController

        setupWithNavController(navigationController)
        setOnItemSelectedListener {
            item - >
                when(item.itemId) {
                    R.id.first_fragment - > navigationController.navigate(R.id.first_fragment)
                    R.id.second_fragment - > navigationController.navigate(R.id.second_fragment)
                    R.id.third_fragment - > navigationController.navigate(R.id.third_fragment)
                    R.id.fourth_fragment - > navigationController.navigate(R.id.fourth_fragment)
                }
            true
        }

    }
}
1 Answers

Try to do the following in the body "when" binding.navigationmenu.get(index: Int).isChecked = true

Related