CodexBloom - Programming Q&A Platform

Kotlin Flow Not Emitting Values After Configuration Change in Android 14

👀 Views: 261 💬 Answers: 1 📅 Created: 2025-06-14
kotlin android flow viewmodel jetpack-compose Kotlin

I'm wondering if anyone has experience with I've searched everywhere and can't find a clear answer... I'm experiencing an scenario where my Kotlin Flow is not emitting values after a configuration change (like screen rotation) in my Android 14 app. I have a ViewModel set up with a MutableStateFlow for managing UI state, but after rotating the device, the Flow seems to stop emitting updates. Here's a simplified version of my ViewModel: ```kotlin class MyViewModel : ViewModel() { private val _state = MutableStateFlow(MyState()) val state: StateFlow<MyState> = _state.asStateFlow() fun updateState(newValue: String) { _state.value = _state.value.copy(value = newValue) } } ``` In my Fragment, I collect the Flow like this: ```kotlin class MyFragment : Fragment() { private val viewModel: MyViewModel by viewModels() override fun onViewCreated(view: View, savedInstanceState: Bundle?) { lifecycleScope.launch { viewModel.state.collect { state -> // Update UI with new state textView.text = state.value } } } } ``` After a configuration change, I noticed that the `collect` block does not execute any further. I’ve tried saving the state in `onSaveInstanceState`, but it seems that the Flow isn't emitting any values anymore. My initial state is being restored correctly, but any updates post-rotation are not reflected in the UI. I've also checked the logs and there are no errors thrown during this process, so I’m not sure where to look next. Has anyone else encountered this scenario? What could be causing the Flow to stop emitting values after a configuration change? Any help would be greatly appreciated! Hoping someone can shed some light on this. Thanks for your help in advance! For context: I'm using Kotlin on Ubuntu 20.04. I'm open to any suggestions.