如何避免更改配置并启动活动后重新加载Admob广告? [英] How to avoid re-loading of Admob ads upon configuration change and starting an Activity?

查看:107
本文介绍了如何避免更改配置并启动活动后重新加载Admob广告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用横幅示例 此处 ,请注意,如果我更改方向,则广告消失了,然后重新加载自身(需要花费时间),因此每次更改方向时,都有一段时间没有任何显示.

Using the banners sample here, I noticed that if I change the orientation, the ad is gone, and then reloads itself (takes time), so there is a time that nothing is shown, each time I change orientation.

我知道可以通过清单来为活动设置configChanges(并且它实际上可以工作),但是通常不建议这样做.它有多个缺陷:

I know it's possible to set configChanges for the activity via the manifest (and it actually works), but this is not a recommended thing to do in general. It has multiple flaws:

  1. 例如,在我的情况下,更改方向可使工具栏保持在相同的高度,这是错误的,因为它在横向上应与纵向上不同.
  2. 进入另一个活动"后,即使Admob视图之前已加载,也仍然会重新加载.
  3. 从纵向转到横向时,反之亦然,广告无法很好地适应新的方向.

在每次配置更改时,还可以采取哪些措施来避免重新加载广告?

What else can be done to avoid re-loading of ads, on each configuration change?

我希望Admob一次加载广告(其余的在后台加载新的),然后显示它们,并在更改方向时避免重新加载.每当我到达时,只要显示最适合当前配置的内容(例如方向)即可.

I want Admob to load ads once (and the rest in the background, for new ones), and then show them, and avoid re-loading when I change orientation. Just show what's best for the current configuration (orientation for example), each time I reach it.

转到具有Admob广告的新活动也是如此.如果之前已加载,则无需重新加载.

Same goes for going to a new Activity that has an Admob ad. If it loaded before, no need to re-load.

我对此颇为赏心,因为Google/Firebase支持人员通过电子邮件告诉我,实际上可以缓存广告:

I've put a bounty on this, because Google/Firebase support told me via email it's actually possible to cache the ads:

Ads SDK将不会为您缓存广告,但是您可以实施 "AdManager"样式类,很可能是单例或实例 附加到您的应用程序实例,可以管理缓存 你.

The Ads SDK won't cache ads for you, however you can implement an 'AdManager' style class, most likely a singleton or an instance attached to your application instance, which can manage caching for you.

您认识到这是销毁并重新创建的Android模式 有关方向的活动,实例状态通常会被保存, 通过捆绑包还原,但这不适用于视图.

As you recognize it's an Android pattern to destroy and recreate activities on orientation, instance state is usually saved and restored via a bundle, but this is not applicable to views.

如果您希望在此优化横幅视图的重新加载时间 情况下,建议您通过AdManager提出广告请求 类,然后AdManager可以应用一些逻辑来缓存广告,例如 作为您的横幅视图一定的时间,然后返回 前一个横幅(如果它对于新请求仍然有效) 会改变方向.

If you wish to optimize the reload time of the banner view in this scenario, I would recommend making your ad request via an AdManager class, and the AdManager can then apply some logic to cache ads such as your banner view for a certain amount of time, and return the previous banner if it is still valid for the new request (which you would make on orientation change).

然后:

SDK并未实现这种缓存广告的想法,因为它是 很难确定适用于所有情况的最佳缓存逻辑, 因此,留给开发人员实施适合他们的缓存 用例,如果需要的话.

This idea of caching ads is not implemented by the SDK, as it is difficult to determine the best caching logic to apply in all cases, thus it is left to the developer to implement caching that suits their use case, if desired.

可能将横幅广告与"AdView"类分离开来 制作更合适的可缓存对象,但这不是 目前是这样.

Possibly having a banner ad decoupled from the 'AdView' class would make for more appropriate cacheable objects, however this is not currently the case.

因此,是的,我们建议为了预取/缓存 横幅广告,您需要存储"AdView".而不是使用 但是,AdManager是另一种受支持且确实是推荐的方法 将使用ViewModel,请参阅 https://developer.android.com/guide/topics/resources/runtime -changes#RetainingAnObject https://developer.android.com/topic/libraries/architecture/viewmodel . 这些都是针对此类用例而设计的.

Therefore as it is, yes we suggest that in order to prefetch / cache a banner ad, you need to store the 'AdView'. Instead of using an AdManager however, another supported and indeed recommended approach would be to use a ViewModel, see https://developer.android.com/guide/topics/resources/runtime-changes#RetainingAnObject and https://developer.android.com/topic/libraries/architecture/viewmodel. These are designed for exactly this kind of use case.

如果确实有可能,我想确切地知道如何去做.请成功的人,在代码中显示可以完成的事情.

If it is indeed possible, I'd like to know exactly how to do it. Please, whoever succeeds in doing it, show in code what can be done.

推荐答案

我不清楚如何缓存普通广告.也不是单例,也不是viewmodel都无济于事,我们不能缓存/保留视图本身,也没有要保留的公共广告对象.

I don't see clear way how to cache ordinary ads. Nor singleton, nor viewmodel won't help, we can't cache/retain the view itself and there is no public ad object to retain.

但是AdLoader可以用来加载原生广告,并保留已加载的UnifiedNativeAd对象.然后,它可以用于在视图中设置广告.

However there is AdLoader using which one can load native ads, and retain loaded UnifiedNativeAd object. Then it can be used to set ad in view.

其他广告sdks可以提供预取广告的方式.

Other ad sdks provide way to prefetch ads though.

P.S.不知道为什么堆栈会在"android viewmodel"查询的搜索结果中出现

P.S. dunno why stack brought this in search results for "android viewmodel" query

这篇关于如何避免更改配置并启动活动后重新加载Admob广告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆