I'm trying to inject a simple Text Banner into an ...
# compose
t
I'm trying to inject a simple Text Banner into an existing Fragment/layout. It embeds fine (per https://developer.android.com/jetpack/compose/interop/interop-apis#compose-in-fragments). But I need to hoist some state (I think) into the fragment. I have a value at the Fragment level in a LiveData. But it's unclear to me how to change my ComposeFunction to become aware of that value and recompose when it changes. I'm successfully able to pass the LiveData as an arg to the @Composable function and compose the original view. But updates to the LiveData don't cause it to recompose. I looked for
observeAsState
per the docs, but it doesn't seem to find that. Nor is it clear where to invoke that at...
i
observeAsState
is part of the
androidx.compose.runtime.livedata
package: https://developer.android.com/reference/kotlin/androidx/compose/runtime/livedata/package-summary
So you'd want to add the
androidx.compose.runtime:runtime-livedata
dependency: https://maven.google.com/web/index.html#androidx.compose.runtime:runtime-livedata
t
Hmmm... Now I wonder if I really want/need a (KT)LiveData. I have a Fragment. I just need a slot of some sort, stored/updated at the Fragment level, that will cause the Compose subelement to recompse when it gets updated. Can I use a MutableState at the Fragment level. Really struggling to get the light bulbs here 😞
a
Yes you can use mutableStateOf in your fragments
z
You can declare top level mutable state object in fragment or in viewModel if you have.
t
🙏