Skip to content

Instantly share code, notes, and snippets.

@attilakruchio
Created June 25, 2022 12:58
Show Gist options
  • Save attilakruchio/19d1ac5bbe5239818b5e40cbdd2c0596 to your computer and use it in GitHub Desktop.
Save attilakruchio/19d1ac5bbe5239818b5e40cbdd2c0596 to your computer and use it in GitHub Desktop.
Gist for 'Easy and elegant navigation with Jetpack Navigation and Hilt' Medium article (https://medium.com/@kruchio98/6f93a0461298)
abstract class BaseFragment : Fragment() {
abstract val viewModel: BaseViewModel
@CallSuper
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.events.collect { event ->
onEvent(event)
}
}
}
}
open fun onEvent(event: SingleEvent) {
when (event) {
is NavigateTo -> {
findNavController().navigate(event.navDirections)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment