I'm implementing Navigation Drawer using androidx.navigation.ui.
Based on Android Studio's Navigation Drawer Activity Template, when I put in onDrawerSlide that works according to the drawer animation, the topLevelDesitnationIds set in AppBarConfigration is ignored, and the navigation button (not the drawer icon, but the back button) will be displayed in the non-topLevel Fragment. Even if I press it, the drawer will come out every time.
val drawerLayout: DrawerLayout = binding.drawerLayout
val navView: NavigationView = binding.navView
val navController = findNavController(R.id.nav_host_fragment_content_main)
//-- START : It will come out a drawer menu regardless of the fragment stack
class LocalActionBarDrawerToggle : ActionBarDrawerToggle(this,
drawerLayout,
binding.appBarMain.toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close) {
override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
// do something
super.onDrawerSlide(drawerView, slideOffset)
}
}
val drawerToggle = LocalActionBarDrawerToggle()
drawerLayout.addDrawerListener(drawerToggle)
drawerToggle.syncState()
//-- END
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_home
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
For non-top level Fragments, the navigation button is a back button instead of a drawer icon, so I think it's simply overriding the motion control. Please give me some advice.
Top app bar
Make sure Toolbar APIs are called before setSupportActionBar().