'Android: topLevelDestinationIds is ignored and the drawer menu appears every time when onDrawerSlide is set
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.
Solution 1:[1]
Caution: If you pass a Toolbar as the argument to setSupportActionBar(), the ActionBar assumes complete ownership of that Toolbar and you must not use any Toolbar APIs after that call. You can use the support for the ActionBar to connect the ActionBar to NavController.
Make sure Toolbar APIs are called before setSupportActionBar().
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mark |
