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

Trym Nilsen

02/28/2024, 2:55 PM
I have an old third party view that I want to add to a composable screen (with AndroidView), located in a tab with a navigation bar. It's a very heavy view that uses some time to initialise and load so the user experience is not great when you switch tabs back and fourth and the composable containing the AndroidView is added and removed causing the view to also be re-created. How might I re-use the view and keep it alive in the background (in scope of the activity its in), to avoid the user waiting and seeing the initialising for it when the tab in the navigation bar is switched? Can I return a reference to the same view for the factory function for multiple different AndroidViews?
s

Sean Proctor

02/28/2024, 3:01 PM
I don't know anything about it, personally, but here's a quote from the
AndroidView
docs
Copy code
To opt-in for View reuse, call the overload of AndroidView that accepts an onReset callback, and provide a non-null implementation for this callback. Since it is expensive to discard and recreate View instances, reusing Views can lead to noticeable performance improvements — especially when building a scrolling list of AndroidViews. It is highly recommended to opt-in to View reuse when possible.
t

Trym Nilsen

02/28/2024, 3:09 PM
I'm unsure if its only for scrolling lists or needs to be supported by the composable that is "hosting" the AndroidView 🤔
s

shikasd

02/29/2024, 12:01 AM
You can just keep this view anywhere in activity/composable scope, Compose reuse likely won't be active here.