I have an app where you can log in, so I want to display different drawer menu options based on if you did so. Untill now I've been trying and I was able to remove items when the user is not logged. However I'm struggling to recover those removed items once log in is performed. Here is the condition where I do this:
navController.addOnDestinationChangedListener { _, destination, _ ->
if (destination.id == R.id.firstFragment ||
destination.id == R.id.loginFragment ||
destination.id == R.id.registerFragment
) {
//drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
navView.menu.removeItem(R.id.firstFragment) //This works
} else {
//drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
navView.menu.add(R.id.firstFragment) //This doesn't work
}
}
CodePudding user response:
If you intend on re-adding all of the items on login, you might as well just hide them instead. It will probably be faster that way anyways.
Here is solution that allows you to hide/unhide a given menu item.
Hide a Navigation Drawer Menu Item - Android
The code navView.menu.add(R.id.firstFragment)
won't work because Menu.add(int)
is looking for a string id, not a layout item id.