Skip to content

Instantly share code, notes, and snippets.

@shawnthye
Last active November 14, 2021 12:16
Show Gist options
  • Save shawnthye/fdb8d34f8447eea59c1e46289c0af995 to your computer and use it in GitHub Desktop.
Save shawnthye/fdb8d34f8447eea59c1e46289c0af995 to your computer and use it in GitHub Desktop.
Workaround for Bottom Navigation with Jetpack Navigation 2.4.0-beta02
override fun onBackPressed() {
/**
* We only override the behavior when back to home from other menu
* else we leave it to the default [onBackPressed]
*/
if (bottomNavigationView.selectedItemId != bottomNavigationView.menu[0].itemId) {
val previousDestination = navController.previousBackStackEntry?.destination ?: run {
/**
* on the root, left it to default [onBackPressed]
*/
return super.onBackPressed()
}
/**
* in case the first menu/tab is not a nested graph
*/
val previousId = previousDestination.parent?.id ?: previousDestination.id
if (previousId == bottomNavigationView.menu[0].itemId) {
/**
* We let [BottomNavigationView] to switch to home
*/
bottomNavigationView.selectedItemId = bottomNavigationView.menu[0].itemId
return
}
}
super.onBackPressed()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment