Andrey K
01/02/2024, 1:40 PMcom.google.android.gms.ads.admanager.AdManagerAdView
and AndroidView Compose Wrapper (the code is in the thread).
It works great when I use test AdManager Ads. But there is an issue in production (please have a look at the screenshot provided):
• Arrow 1 - the video of the Ad Banner is not displayed after the Ad Banner is bind and shown.
• Arrow 2 - If I move the app to the background and then to the foreground (or if I scroll the Ad Banner off and back on screen), the video is shown.
If I wait for 5 mins or more after binding and showing the Ad Banner, the video won't appear - so I think that some (screen) event/method call is missing.
I found the adView.rootView.requestLayout() workaround for the other Ad Banner issue but it didn't help me. I also tried to call invalidate()
, postInvalidate()
and requestFocus()
but nothing helped. Cannot understand what (screen) event is missing for the Ad Banner to show a video by default.
Can you please suggest me how to fix this issue? Any ideas are appreciated!Andrey K
01/02/2024, 1:40 PM@Composable
fun AdManagerAdBanner(
adUnit: AdManagerUnitUiModel,
modifier: Modifier = Modifier,
) {
Box {
AndroidView(
modifier = modifier.fillMaxWidth(),
factory = { context ->
AdManagerAdView(context).apply {
setAdSizes(adUnit.adSizes)
adUnitId = adUnit.unitId
val adRequest = AdRequest.Builder().build()
loadAd(adRequest)
}
},
)
}
}
FranSoto
01/02/2024, 4:06 PMFranSoto
01/02/2024, 4:08 PMFranSoto
01/02/2024, 4:08 PMAndrey K
01/02/2024, 4:08 PMonAdLoaded()
success callback is invokedFranSoto
01/02/2024, 4:09 PMAndrey K
01/02/2024, 4:10 PMFranSoto
01/02/2024, 4:10 PMFranSoto
01/02/2024, 4:11 PMFranSoto
01/02/2024, 4:14 PMFranSoto
01/02/2024, 4:14 PMFranSoto
01/02/2024, 4:15 PMFranSoto
01/02/2024, 4:15 PMAndrey K
01/02/2024, 4:16 PMAndrey K
01/02/2024, 4:26 PMrequestLayout
doesn't take effect if I have several inner callbacks. Let me carefully check my latest changes and I will update the thread with the up-to-date info.
Btw, thanks a lot for your suggestions! doOnLayout
is probably not the best idea but I quite interesting 🙂Andrey K
01/02/2024, 4:52 PMFranSoto
01/02/2024, 5:32 PMFranSoto
01/02/2024, 5:32 PMFranSoto
01/02/2024, 5:32 PMAndrey K
01/02/2024, 5:33 PMFranSoto
01/02/2024, 5:34 PMAndrey K
01/02/2024, 5:35 PMFranSoto
01/02/2024, 5:39 PMFranSoto
01/02/2024, 5:41 PMAndrey K
01/02/2024, 5:45 PMvideoController.hasVideoContent()
returns false in both cases:
• if video is shown by default
• or if it is not shown
I messed up with the pause / resume / destroy ad lifecycle.
Compose's lifecycle don't match exactly view's lifecycleI also feel that the issue might be related to lifecycle events and
requestLayout()
helps to decrease amount of not-shown animated parts. But cannot think of a better fix for now 😞 Hope there is another API or anything else that could solve this issue completely - like as I move the app to background and then to the foregroundFranSoto
01/02/2024, 5:55 PMAndrey K
01/02/2024, 5:56 PMFranSoto
01/02/2024, 5:59 PMAndrey K
01/02/2024, 6:25 PMFranSoto
01/02/2024, 6:50 PMAndrey K
01/03/2024, 7:46 AM@Composable
fun AdManagerAdBanner(
adUnit: AdManagerUnitUiModel,
modifier: Modifier = Modifier,
) {
val text = remember { mutableStateOf("foo") }
val context = LocalContext.current
val adView = remember {
AdManagerAdView(context).apply {
setAdSizes(adUnit.adSizes)
adUnitId = adUnit.unitId
adListener = object : AdListener() {
override fun onAdLoaded() {
super.onAdLoaded()
rootView.requestLayout()
text.value = "bar"
}
}
val adRequest = AdRequest.Builder().build()
loadAd(adRequest)
}
}
key(text.value) {
AndroidView(
modifier = modifier.fillMaxWidth(),
factory = {
adView.parentViewGroup?.removeView(adView)
adView
},
)
}
}