In my project I use JetPack Compose and the AndroidView to use an XML View.
@Composable
fun MyComposable(
message: String
) {
AndroidView(
factory = { context ->
TextView(context).apply {
text = message
}
})
}
My issue is that when my message state change, the XML view in the AndroidView isn't recomposed. There is an option in the AndroidView to obverse the state change ?
ps: I've simplified MyComposable for the example
You can use the update block.
From the doc:
The
updateblock can be run multiple times (on the UI thread as well) due to recomposition, and it is the right place to setViewproperties depending on state. When state changes, the block will be reexecuted to set the new properties. Note the block will also be ran once right after thefactoryblock completes
AndroidView(
factory = { context ->
TextView(context).apply {
text = "Initial Value"
}
},
update = {
it.text = message
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With