Skip to content

Instantly share code, notes, and snippets.

@Zhuinden
Created June 19, 2020 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zhuinden/65edfbbb85bb095664fe8f938a635a88 to your computer and use it in GitHub Desktop.
Save Zhuinden/65edfbbb85bb095664fe8f938a635a88 to your computer and use it in GitHub Desktop.
Hilt NavigationDispatcher
@ActivityRetainedScoped
class NavigationDispatcher @Inject constructor() {
private val navigationEmitter: EventEmitter<NavigationCommand> = EventEmitter()
val navigationCommands: EventSource<NavigationCommand> = navigationEmitter
fun emit(navigationCommand: NavigationCommand) {
navigationEmitter.emit(navigationCommand)
}
}
@uberchilly
Copy link

The idea of this sample is to use it in viewmodel to navigate, something like this
navigationDispatcher.emit { navController -> navController.navigateUp() }
for example. How to use result API and return something when fragment is required and there is no way to get fragment from navController? Did you have a need for this? I ended up using an older API something like
previousBackStackEntry?.savedStateHandle?.set(key, result)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment