Reputation: 49
I am building an app with android jetpack navigation and fragments. I would like to include a navigation drawer in my app but I am unsure about where exactly I would put my DrawerLayout tag. I want the nav drawer to be accessible from most (but not all) fragments. Do I put this tag in activity_main.xml or in every layout file that I would like to include a nav drawer in? Thank you!
Upvotes: 0
Views: 48
Reputation: 355
In your activity_main.xml
wrap your main layout with DrawerLayout
. Since you want it to be accessible from anywhere then you don't need to add addOnDestinationChangedListener
on navController. But if you don't want on specific fragment then you have to add it and put condition to check that NavDestination and lock it as below
navController.addOnDestinationChangedListener { nc: NavController, nd: NavDestination, _: Bundle? ->
if (nd.id == nc.graph.startDestination) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
} else {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
}
}
Upvotes: 1