@Serializable
data class MyUiState(
// Persistent
val data: List<String> = emptyList(),
val selectedItem: String? = null,
// Transient
val isLoading: Boolean = false,
val error: String? = null
)
class MyViewModel(private val savedStateHandle: SavedStateHandle) : ViewModel() {
// Define which properties are transient
private val transientProps = listOf("isLoading", "error")
// Declare your state flow - Reanimator handles the rest!
private val _uiState by savedStateHandle.getMutableStateFlow(
defaultValue = MyUiState(), // Initial/default state
coroutineScope = viewModelScope, // Scope for saving changes
transientProperties = transientProps // What NOT to save
// key = "custom_state_key" // Optional: custom key
)
val uiState: StateFlow<MyUiState> = _uiState.asStateFlow()
// ... rest of your ViewModel logic ...
fun updateData(newData: List<String>) {
// Just update the state - Reanimator saves persistent parts automatically
_uiState.update { it.copy(data = newData, isLoading = false) }
}
}
#android #kmp