Reputation: 26821
I have a ViewModel which takes a SavedStateHandle
parameter. I am saving a String in there like this:
private fun saveString(str: String) {
state.set(KEY_STRING, str)
}
Then I force-close my app and relaunch it, and want to retrieve the saved string like this:
fun getSavedString(): String? {
return state.get<String>(KEY_String)
}
However, it always returns null. Any ideas how to use SavedStateHandle correctly?
Upvotes: 6
Views: 2383
Reputation: 104
"Then I force-close my app and relaunch it.."
I guess you kill the app completely =)
One of the way for reproducing saveState
case you need:
Don't keep activities
No background processes
SavedStateHandle
will saved)SavedStateHandle
will restored)Another the easiest way:
Also you should use
SavedStateViewModelFactory
if you want to receiveSavedStateHandle
inViewModel
Upvotes: 3