how to to continues State in Jetpack Compose on Configuration Change with ViewModel and SavedStateHandle
I've been struggling with this for a few days now and could really use some help. I'm writing unit tests and I've searched everywhere and can't find a clear answer. I'm working on an Android application using Jetpack Compose and ViewModel with SavedStateHandle to continue UI state across configuration changes. However, I've encountered an scenario where the state does not continue as expected, resulting in the loss of user input when the device is rotated. For instance, I have a simple form with a text field bound to my ViewModel state. Here's a snippet of my ViewModel: ```kotlin class MyViewModel(private val savedStateHandle: SavedStateHandle) : ViewModel() { var userInput: String get() = savedStateHandle.get<String>("userInput") ?: "" set(value) { savedStateHandle.set("userInput", value) } init { // Restore state if available userInput = savedStateHandle.get<String>("userInput") ?: "" } } ``` And in my Composable function, I have: ```kotlin @Composable fun MyForm(viewModel: MyViewModel) { var text by remember { mutableStateOf(viewModel.userInput) } TextField( value = text, onValueChange = { newText -> text = newText viewModel.userInput = newText }, label = { Text("Enter something...") } ) } ``` When I rotate the device, I expect the text field to retain the input, but it resets to an empty string. I've tried debugging, and it seems that the ViewModel is being recreated, and the SavedStateHandle is not restoring the state correctly. I also checked if the ViewModel is scoped correctly to the activity and not to the composable. My current setup in the activity looks like this: ```kotlin val viewModel: MyViewModel = viewModel() ``` So, I'm unsure why the state isn't persisting. I’ve also verified that I’m using Jetpack Compose version 1.1.1 and the ViewModel and SavedStateHandle from AndroidX Lifecycle version 2.4.0. Any ideas on what might be going wrong or how to resolve this scenario? I'm working on a API that needs to handle this. I'm using Kotlin latest in this project. Thanks for your help in advance! I'm developing on Ubuntu 22.04 with Kotlin. I appreciate any insights!