I am trying a simple flow, from my activity I open the file picker then to viewmodel and this gives me a the following crash:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
And here is the code that creates this crash:
private val mainViewModel: MainViewModel by viewModels()
private var activityResultLaunch = registerForActivityResult(StartActivityForResult()) { result ->
val fileUri = result.data?.data ?: return@registerForActivityResult
val stream = contentResolver.openInputStream(fileUri) ?: return@registerForActivityResult
uploadFiles(stream)
}
private fun uploadFiles(stream: InputStream) {
lifecycleScope.launchWhenStarted {
mainViewModel.uploadFiles(
stream = stream
).asLiveData().observe(this@MainActivity, {
handleFileUploadStatus(it)
})
}
}
@HiltViewModel
class MainViewModel @Inject constructor(
private val filesRepository: FilesRepository
) : ViewModel() {
suspend fun uploadFiles(stream: InputStream): Flow<UploadStatusUI> {
return filesRepository.uploadFiles(listOf(stream))
}
}
After some research I found this issue on Google's issue tracker here and another issue on firefox's github here but nothing worked. Apparently the issue got fixed on lifecycle 2.3.1 but I am still facing it.
Here are my versions:
"androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"
"androidx.lifecycle:lifecycle-viewmodel-savedstate:2.4.0"
Thanks in advance!
Edit: Crash stack trace here