ViewModel not retaining data after screen rotation in Android 14 when using SavedStateHandle
I'm updating my dependencies and Quick question that's been bugging me - I'm working on a project and hit a roadblock....... I'm working with an scenario where my ViewModel is not retaining data after a screen rotation, despite using `SavedStateHandle`. I'm targeting Android 14 and using Hilt for dependency injection. My ViewModel has a MutableLiveData that I expect to continue through configuration changes using SavedStateHandle, but it appears to reset its value after the rotation. Here's a simplified version of my ViewModel: ```kotlin @HiltViewModel class MyViewModel @Inject constructor( private val savedStateHandle: SavedStateHandle ) : ViewModel() { var myData: MutableLiveData<String> = savedStateHandle.getLiveData("myData") fun updateData(newData: String) { myData.value = newData savedStateHandle.set("myData", newData) } } ``` In my Fragment, I observe the LiveData like this: ```kotlin class MyFragment : Fragment() { private val viewModel: MyViewModel by viewModels() override fun onViewCreated(view: View, savedInstanceState: Bundle?) { viewModel.myData.observe(viewLifecycleOwner) { data -> // Update UI with the data } } } ``` When I update `myData` and then rotate the screen, the observer is called but `myData` is null. I've tried to debug it and noticed that the `SavedStateHandle` seems to be losing its state during the configuration change. I ensured that I don't call `updateData` during initialization in the Fragment. Also, I've verified that the lifecycle of the Fragment and ViewModel are correctly scoped and that I'm not creating a new instance of the ViewModel. This behavior is really puzzling, and I need to figure out how to resolve it. Any insights on why the data might be lost or how to properly use `SavedStateHandle` in this context would be greatly appreciated! What's the best practice here? My development environment is Windows 10. Is this even possible?