Skip to content

Instantly share code, notes, and snippets.

@fvilarino
Last active April 20, 2021 19:14
Show Gist options
  • Save fvilarino/300a021745078d751a48478a5e6a9378 to your computer and use it in GitHub Desktop.
Save fvilarino/300a021745078d751a48478a5e6a9378 to your computer and use it in GitHub Desktop.
View System Viewmodel Observer
@HiltViewModel
class CityViewModel @Inject constructor() : ViewModel(), DefaultLifecycleObserver {
override fun onStart(owner: LifecycleOwner) {
// fragment is now started, run tasks
}
override fun onStop(owner: LifecycleOwner) {
// fragment is now stopped, cancel running tasks
}
}
class CityFragment : Fragment() {
private val viewModel: CityViewModel by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
lifecycle.addObserver(viewModel)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment