https://kotlinlang.org logo
#compose
Title
# compose
a

Alex

10/05/2021, 9:38 AM
Suppose I am using a
ComposeView
to show a composable. Is there a way for the
ComposeView
to receive a callback when the content is recomposed?
Receiving a callback when the content of the
ComposeView
is being redrawn would also work
r

raenardev

10/05/2021, 10:58 AM
I think you can use
SideEffect
in your top composable function inside ComposeView for that.
Schedule 
effect
 to run when the current composition completes successfully and applies changes.
https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#SideEffect(kotlin.Function0)
a

Alex

10/05/2021, 11:37 AM
Brillant, cheers!
a

Adam Powell

10/05/2021, 1:28 PM
Why do you want this?
a

Alex

10/07/2021, 9:35 AM
We have a blurring feature, implemented for Android < 12 as a custom view
I need to rell the blurring custom view when it should invalidate itself (e.g. when its content has changed in compose)
a

Adam Powell

10/07/2021, 1:06 PM
SideEffect won't be enough for you then as it only runs if that specific call to SideEffect was recomposed. Layout and drawing frequently change without recomposing.
a

Alex

10/08/2021, 6:56 AM
Hm, what would be a way to achieve it in compose then? We managed it inside of the custom blurring view now, skipping compose for that.
a

Adam Powell

10/08/2021, 2:13 PM
This is all going to be lower level than compose for your purposes. ViewTreeObserver callbacks are likely to be the closest thing you can rely on from public API since compose UI will use multiple views internally; there's no single view you can extend or add callbacks to that would give you a complete picture or be guaranteed not to change view tree structure in a future update.
If you'd like to file a feature request describing the use case we can track it but it's not likely to be prioritized highly when ranked against other pending work to support other view integration features.
3 Views