简体   繁体   English

如何在 Jetpack Compose 中为屏幕加载一个广告

[英]How to load one ad for screen in Jetpack Compose

This is my code:这是我的代码:

AndroidView(
        modifier = modifier,
        factory = {
            val view =
                LayoutInflater.from(context).inflate(R.layout.native_ad, null, true)
            val adView = view.findViewById<TemplateView>(R.id.medium_ad_template)
            
            adView.apply {
                AdLoader.Builder(context, context.getString(AR.string.native_unit_id))
                    .forNativeAd { nativeAd ->
                        setStyles(NativeTemplateStyle.Builder().build())
                        setNativeAd(nativeAd)
                    }
                    .build()
                    .loadAd(request)
            }

            view
        }
    )

native_ad.xml native_ad.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.ads.nativetemplates.TemplateView
        android:id="@+id/medium_ad_template"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:gnt_template_type="@layout/gnt_medium_template_view" />
</LinearLayout>

The ad keeps refreshing on screen navigation广告在屏幕导航上不断刷新

I tried to set a tag on the first load and keep check in it to see if the ad already loaded, but the view refreshed and the tag resets to the default value我尝试在第一次加载时设置一个标签并继续检查它以查看广告是否已加载,但视图已刷新且标签重置为默认值

In your case I think view is recreated because your flag value is reset while you change the screen to stop that try rememberSaveable as below.在您的情况下,我认为重新创建视图是因为在您更改屏幕以停止尝试rememberSaveable时重置标志值,如下所示。

rememberSaveable which is similar to remember, but the stored value can survive process death or configuration changes. rememberSaveable与 remember 类似,但存储的值可以在进程死亡或配置更改后继续存在。

val flag: MutableState<Boolean> = rememberSaveable { mutableStateOf(false) }
  LaunchedEffect(key1 = flag ){
       AndroidView(...)
  }

I hope It will work我希望它会起作用

It looks like you need some flag that will remember that will allow to control for not reloading ad again.看起来您需要一些可以记住的标志,以便控制不再重新加载广告。 so have you checked with rememberUpdatedState所以你检查过rememberUpdatedState

here is link of documentation for same这是相同文件的链接

https://developer.android.com/jetpack/compose/side-effects#rememberupdatedstate https://developer.android.com/jetpack/compose/side-effects#rememberupdatedstate

By what I could understand about your question, Side effect is what you would be needing here.根据我对你的问题的理解,副作用就是你在这里需要的。 Put AndroidView method inside a Side effect block and define what you would like to update the block.将 AndroidView 方法放在 Side effect 块中,并定义您想要更新块的内容。

  LaunchedEffect(key1 = <On change of this your block is recomposed>){
       AndroidView(...)
  }

Note: If you don't want the block to update then just put a string in place of the key in launched effect.注意:如果您不希望块更新,则只需在启动效果中放置一个字符串代替键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM