Hi folks. I wanted to observe a livedata value. I started the process in a composable and then I swi...
a
Hi folks. I wanted to observe a livedata value. I started the process in a composable and then I switched the screen. If I don't switch the screen, I can observe the changing livedata in the same composable. But when I switched it, I cannot observe anymore. Do you guys have any idea?
m
What is your usecase? What do you want to achieve?
a
i'm observing workinfo livedata provided by workmanager
m
why you need to keep observing it when you go to another screen/composable?
a
I will create a progress bar on main screen
this is an upload process
m
if you are observing the value wouldn’t it be updated when you go back to that screen?
a
Yes this is the problem. It is working but i cannot observe it anymore.
m
are you using
observeAsState()
?
a
Yes
m
is it possible to share code?
a
yes
Copy code
@Composable
fun UploadProgressBar() {
    val context = LocalContext.current
    val viewModel = LocalViewModel.current
    val progressResult = viewModel.progressWorkInfo.observeAsState()

    progressResult.value?.let { result ->
        when (result.state) {
            WorkInfo.State.RUNNING -> {
                val progress = result.progress.getFloat(PROGRESS, 0f)

                LinearProgressIndicator(
                    progress = progress,
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(4.dp),
                    color = primaryColor
                )

                Timber.d("progressResult: %s", progress)
            }
            WorkInfo.State.SUCCEEDED -> {
                val flash = progressResult.value!!.outputData.getString(TAG_FLASH)
                val success = progressResult.value!!.outputData.getBoolean(TAG_SUCCESS, false)
                ContextUtils.showToast(
                    context,
                    flash.toString(),
                )
                Timber.d("progressSuccess: %s", success)
            }
            WorkInfo.State.FAILED -> {
                Timber.d("WORKINFO FAIL : %s", result.outputData.getString(TAG_FLASH))
                ContextUtils.showToast(
                    context,
                    R.string.an_error_occurred,
                )
            }
            else -> {
            }
        }
    }
}
Normally it's working on the screen which I started to process on it.
m
I’ll take a look
a
Change
val progressResult = viewModel.progressWorkInfo.observeAsState()
to
val progressResult by viewModel.progressWorkInfo.observeAsState()
Nevermind.. That doesn't solve anything
a
I hoped for a moment 😄
z
How are you implementing navigation? Jetpack Navigation or something else?
s
At a glance, that progressResult parameter is suspicious to me. Most likely the bug is in the caller or LocalViewModel setup causing you to observe the wrong LiveData.
a
@Zach Klippenstein (he/him) [MOD] I used androidx.navigation.compose, so yes it is Jetpack Navigation