In my recent project, I'm using Navigation component with BottomNavBar. BottomNavBar has 4 menus. For the first 3 menus, I've defined fragments in NavGraph file, but for the last menu item, I've to open Drawer.
Now click listener for the first 3 menus is working fine as all three fragments are opening. But I'm not able to open drawer.
And here is the code
private fun initNavigation() {
val host : NavHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment? ?: return
binding.bottomNav.setupWithNavController(host.navController)
}
I've also tried adding NavigationItemSelectedListener like this but app lost in handling backStack. As by pressing back button, All previously opened fragments were coming again and again.
binding.bottomNav.setOnNavigationItemSelectedListener {
when(it.itemId){
R.id.homeFragment->{
navController.navigate(R.id.homeFragment)
true
}
R.id.latestFragment->{
navController.navigate(R.id.latestFragment)
true
}
R.id.cartFragment->{
navController.navigate(R.id.cartFragment)
true
}
else->{
binding.drawerLayout.openDrawer(GravityCompat.START)
false //Don't want to check more menu item
}
}
}
Please anyone suggest any idea.
Finally came to this solution, however still waiting for the more optimized solution.
Also had need to explicitly check menu items.