Is this the correct way to make sure my view is de...
# compose
s
Is this the correct way to make sure my view is destroyed when it leaves the composition?
Copy code
var bannerView by remember { mutableStateOf<MoPubView?>(null) }
val context = LocalContext.current
DisposableEffect(null) {
    bannerView = MoPubView(context).also {
        it.setAdUnitId("...")
        it.adSize = MoPubView.MoPubAdSize.HEIGHT_50
        it.loadAd()
    }

    onDispose {
        bannerView?.destroy()
        bannerView = null
    }
}

bannerView?.let { view ->
    AndroidView(
        factory = { view }
    )
}
z
You’ll skip a frame there before adding your view, since you won’t create it until after the first composition is done. Better to create and initialize your view inside the factory function you’re passing to AndroidView, and then store it in a mutable state or other holder type from there. Then you could still use DisposableEffect to call destroy.
👍 1
s
Alright, thanks!
z
FWIW I filed an feature request to make this easier to do if you wanna star it: https://issuetracker.google.com/208439888
✔️ 1