Skip to content

Instantly share code, notes, and snippets.

@safaorhan
Created April 21, 2022 19:25
Show Gist options
  • Save safaorhan/70216fa8edd47c3b7106dabcc7803293 to your computer and use it in GitHub Desktop.
Save safaorhan/70216fa8edd47c3b7106dabcc7803293 to your computer and use it in GitHub Desktop.
Log the fragment back stack status with each change for debugging purposes.
// Adapted from: https://stackoverflow.com/a/67267584/3539594
private fun listenNavigationChanges() {
val navHost = supportFragmentManager.findFragmentById(R.id.container) as NavHostFragment
val navController = navHost.navController
navController.addOnDestinationChangedListener { _, destination, _ ->
val breadcrumb = navController
.backQueue
.map {
it.destination
}
.filterNot {
it is NavGraph
}
.joinToString(" > ") {
it.displayName.split('/')[1]
}
println(breadcrumb)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment