I have a live data that I am observing in my fragment, in which I have to act on the result received from LiveData and update my MutableState which is used in a composable.
I am not able to figure out why the MutableState value is not getting changed.
Here is how I am doing it:
viewModel.urlLiveData.observe(
this@MyFragment as LifecycleOwner
) {
val url = if (!it.isNullOrEmpty()) {
//perform operations on the string received from live data (it)
} else if (!someVariable.isNullOrEmpty()) {
someVariable
} else null
url?.let { finalUrl ->
Log.d("LOG_TAG", "url1: ${finalUrl}")
urlMutableState.value = UrlMutableState(url = finalUrl)
Log.d("LOG_TAG", "url2: ${urlMutableState?.value?.url}")
}
}
The logcat shows different URLs for url1 and url2 logs. Ideally, it must be same as I am assigning my MutableState a new value.
Here are the logs:
D/LOG_TAG: url1: javascript:myCallback( )
D/LOG_TAG: url2: https://someUrl.com/page1
Here, url2 holds the old value of mutableState instead of the new value.