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

Peter Mandeljc

01/19/2022, 10:27 AM
is it okay to return constant view in AndroidView factory?
Copy code
val btn = Button(context)
AndroidView(factory = { btn } )
I guess it could impact performance, when stuff is re-composed. Anything else?
c

cb

01/19/2022, 11:23 AM
You'll want to remember the
Button
, but that should work fine. We do something similar.
🍻 1
z

Zoltan Demant

01/19/2022, 11:51 AM
Why would you do this? Im just very curious
c

cb

01/19/2022, 12:05 PM
Otherwise you'll be creating a new view on every recomposition.
z

Zoltan Demant

01/19/2022, 12:07 PM
My bad, I was referring to why one would instantiate the view outside of
AndroidView
? I understand that might also be to re-use the view, but AndroidView does that as well doesnt it?
c

cb

01/19/2022, 12:27 PM
Ahhh, we had a use case where we needed the view before the `AndroidView`:
Copy code
val view = remember(context) { BlahBlahView(context) }

Box(
    modifier = modifier
        .nestedScroll(rememberViewInteropNestedScrollConnection(view))
) {
    AndroidView(
        factory = { view },
        modifier = Modifier.matchParentSize(),
    )
    
    content()
}
z

Zoltan Demant

01/19/2022, 12:40 PM
Ah, cool. Thanks for explaining! 🙏🏽
5 Views