Skip to content

Instantly share code, notes, and snippets.

@ceruleanotter
Created June 20, 2019 18:53
Show Gist options
  • Save ceruleanotter/89afe643a436e8a96ebdd8497d7afe52 to your computer and use it in GitHub Desktop.
Save ceruleanotter/89afe643a436e8a96ebdd8497d7afe52 to your computer and use it in GitHub Desktop.
ViewModel Integrations: Step 3 Saved State Module
class MyViewModel(state : SavedStateHandle) : ViewModel() {
// Keep the key as a constant
companion object {
private val USER_KEY = "userId"
}
private val savedStateHandle = state
fun saveCurrentUser(userId: String) {
// Sets a new value for the object associated to the key.
savedStateHandle.set(USER_KEY, userId)
}
fun getCurrentUser(): String {
// Gets the current value of the user id from the saved state handle
return savedStateHandle.get(USER_KEY)?: ""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment