Skip to content

Instantly share code, notes, and snippets.

@AdamMc331
Created June 8, 2021 21:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamMc331/12b4ea2424715cd53334714c6177e39c to your computer and use it in GitHub Desktop.
Save AdamMc331/12b4ea2424715cd53334714c6177e39c to your computer and use it in GitHub Desktop.
Example for navigating with ViewModel to Fragment
class Fragment : Fragment() {
fun listenForNavigation() {
lifecycleScope.launchWhenResumed {
val directions = viewModel.navigationChannel.receive()
findNavController().navigate(directions)
}
}
}
class MyViewModel : ViewModel() {
val navigationChannel: Channel<NavDirections> = Channel()
fun navigate() {
viewModelScope.launch {
navigationChannel.send(FragmentDirections.navigateToProfile())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment