skip to Main Content

I am using navigation component for bottom navigation component.

bottomNavbar.setupWithNavController(navController)

now this is working fine but when I hit the back button, it is returning to the home page but the icon is not changing, it is stuck in the previous selected fragment. I have three fragments and I have implemented navbar separately in all those fragments, here’s the code for those three fragments.

settings fragment

val bottomNavbar = view.findViewById<BottomNavigationView>(R.id.bottomNavbar)
bottomNavbar.setupWithNavController(navController)

search fragment

    val bottomNavbar = view.findViewById<BottomNavigationView>(R.id.bottomNavbarSearch)
    bottomNavbar.setupWithNavController(navController)

chat fragment

val bottomNavbar = view.findViewById<BottomNavigationView>(R.id.bottomNavbar)
    bottomNavbar.setupWithNavController(navController)

here search fragment is my home fragment.

Is there a mistake in my implementation or should I just switch to the old way of implementing bottom navigation view.

any help is appreciated. Thanks

2

Answers


  1. Make sure to have the same ids in the bottomNavView menu that matches
    the corresponding ones in the navGraph of the bottomNavView fragments.

    check this answer

    Login or Signup to reply.
  2. It seems you have added BottomNavigationView on all three fragments which should ideally be on Activity’s view below your fragment/FragmentContainerView where you have defined your navGraph.

    To fix it you need to remove BottomNavigationView from all 3 fragments and add it on Activity using the following basic structure.

    Activity XML structure:

    <constriantLayout>
        <fragment/> //with navGraph
        <bottomNavigationView/>
    </constraintLayout>
    

    and onCreate of Activity use the code setting up BottomNavigationView

    val bottomNavbar = findViewById<BottomNavigationView>(R.id.bottomNavbar)
    bottomNavbar.setupWithNavController(navController)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search