Last active
February 22, 2021 00:05
-
-
Save fergusonm/b16e9c917deb15b683a87af5ab04396d to your computer and use it in GitHub Desktop.
Fragment flow observer usage
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
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