Skip to content

Instantly share code, notes, and snippets.

@mlykotom
Last active January 3, 2020 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlykotom/e167b3ff4c591094be3e034293e93b8a to your computer and use it in GitHub Desktop.
Save mlykotom/e167b3ff4c591094be3e034293e93b8a to your computer and use it in GitHub Desktop.
Connecting The Dots :: Pure SavedState - ViewModel body
class SomeViewModel(/* ..ommited.. */){
val counter = MutableLiveData<Int>(0)
init {
counter.value = savedStateHandle.get("counter") ?: 0
counter.observeForever { newValue ->
savedStateHandle.set("counter", newValue)
}
}
fun onPlusClick() {
counter.value = (counter.value ?: 0) + 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment