Thread
#compose
    s

    Se7eN

    1 year ago
    Is this the correct way to make sure my view is destroyed when it leaves the composition?
    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 }
        )
    }
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    1 year ago
    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.
    s

    Se7eN

    1 year ago
    Alright, thanks!
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    9 months ago
    FWIW I filed an feature request to make this easier to do if you wanna star it: https://issuetracker.google.com/208439888