Is it possible to prevent an AndroidView from recomposing if a parent node recomposes? I have an And...
a
Is it possible to prevent an AndroidView from recomposing if a parent node recomposes? I have an AndroidView wrapping a camera2 PreviewView to display the camera feed. When a parent node recomposes I see that the PreviewView blinks black, which tells me that the view is being recreated.
a
Just spit-balling here, but you might be able to wrap the composable definition in a key(x){} where x is something contextual, or unit 😬
a
can you show your code? generally recomposing an
AndroidView
composable does not recreate the view. There may be something else going on there.
a
@Adam Powell I was lazy. I only read the doc for the
update
param which says "The callback to be invoked after the layout is inflated.", so I thought it was an adequate place to set the preview surface. Reading the docs for
AndroidView
a bit further I found "TheĀ 
update
Ā block can be run multiple times (on the UI thread as well) due to recomposition, and it is the right place to setĀ 
View
Ā properties depending on state". Then I realized what was my mistake, and I moved everything to
factory
. I guess the doc for
update
should be changed to something like
the callback to update the view when there is a recomposition
. Should I create a PR to update the docs?
šŸ‘ 1
a
Since this led to some confusion for you I think it's important that the docs are especially precise here. The
update
block will be run if: 1. the view is initially created by the supplied factory 2. a new
update
is provided through recomposition (e.g. recomposed with a new/different lambda capture) 3. snapshot state accessed by
update
was invalidated
I purposely have not used the word, "when" in this description; the promise of this API contract only calls for running
update
after one of the above conditions has been met and before any upcoming frame performs layout and drawing
a
Thanks @Adam Powell
484 Views