Skip to content

Instantly share code, notes, and snippets.

@attilakruchio
Created June 25, 2022 13:03
Show Gist options
  • Save attilakruchio/73887c7ffbc21b1c63f9f7415ab51eb8 to your computer and use it in GitHub Desktop.
Save attilakruchio/73887c7ffbc21b1c63f9f7415ab51eb8 to your computer and use it in GitHub Desktop.
Gist for 'Easy and elegant navigation with Jetpack Navigation and Hilt' Medium article (https://medium.com/@kruchio98/6f93a0461298)
fun Navigator.setupWithNavController(
activity: ComponentActivity, navController: NavController
) {
activity.lifecycleScope.launch {
activity.repeatOnLifecycle(Lifecycle.State.STARTED) {
navEvents.collect { direction ->
when (direction) {
is Navigator.Direction.NavigateTo -> {
navController.navigate(direction.navDirections)
}
is Navigator.Direction.NavigateBack -> {
if (!navController.popBackStack()) {
activity.finish()
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment