Created
June 8, 2021 21:38
-
-
Save AdamMc331/12b4ea2424715cd53334714c6177e39c to your computer and use it in GitHub Desktop.
Example for navigating with ViewModel to Fragment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Fragment : Fragment() { | |
fun listenForNavigation() { | |
lifecycleScope.launchWhenResumed { | |
val directions = viewModel.navigationChannel.receive() | |
findNavController().navigate(directions) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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