Last active
April 20, 2021 19:14
-
-
Save fvilarino/300a021745078d751a48478a5e6a9378 to your computer and use it in GitHub Desktop.
View System Viewmodel Observer
This file contains 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
@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