Skip to content

Instantly share code, notes, and snippets.

@fergusonm
Last active February 22, 2021 00:05
Show Gist options
  • Save fergusonm/b16e9c917deb15b683a87af5ab04396d to your computer and use it in GitHub Desktop.
Save fergusonm/b16e9c917deb15b683a87af5ab04396d to your computer and use it in GitHub Desktop.
Fragment flow observer usage
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.eventsFlow
.onEach {
when (it) {
MainViewModel.Event.NavigateToSettings -> {}
is MainViewModel.Event.ShowSnackBar -> {}
is MainViewModel.Event.ShowToast -> {}
}
}
.observeInLifecycle(this)
}
// OR if you prefer a slightly tighter lifecycle observer:
// Be sure to use the right lifecycle owner in each spot.
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.eventsFlow
.onEach {
when (it) {
MainViewModel.Event.NavigateToSettings -> {}
is MainViewModel.Event.ShowSnackBar -> {}
is MainViewModel.Event.ShowToast -> {}
}
}
.observeInLifecycle(viewLifecycleOwner)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment