Created
June 20, 2019 18:53
-
-
Save ceruleanotter/89afe643a436e8a96ebdd8497d7afe52 to your computer and use it in GitHub Desktop.
ViewModel Integrations: Step 3 Saved State Module
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
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