So i’m curious how you can create a view model backed by MutableState but still be able to have the data saved to the savedStateHandle. With livedata i could just back the entire property by the saved state handle, assuming the value was parcelable.
Copy code
data class UIState(val startTime: Long = System.currentTimeMillis())
class UIStateVM(savedStateHandle: SavedStateHandle): ViewModel() {
private val _uiState = mutableStateOf(UIState())
val uiState: State<UIState> get() = _uiState
}
mattinger
10/27/2021, 7:49 PM
I could do this manually, but it seems like there should be a better way:
Copy code
init {
savedStateHandle.get<UIState>("uiState")?.let { state ->
_uiState.value = state
}
viewModelScope.launch {
snapshotFlow { uiState.value }.collect { state ->
savedStateHandle.set("uiState", state)
}
}
}
a
Alex Vanyo
10/27/2021, 9:41 PM
There currently isn’t a better built-in way, unfortunately. A complete solution would also likely take advantage of the