0

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 Answers2

0

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

John Njuki
  • 11
  • 2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32944636) – Gabriele Mariotti Oct 20 '22 at 12:11
0

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)
nitinkumarp
  • 2,120
  • 1
  • 21
  • 30