android – 如何避免在配置更改和启动活动时重新加载Admob广告?

使用横幅样本 here,我注意到如果我改变方向,广告就会消失,然后重新加载(需要时间),所以每当我改变方向时,都会有时间显示任何内容.

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

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

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

我希望Admob一次加载广告(其余部分在后台加载新广告),然后展示,并避免在我更改方向时重新加载.每次到达时,只显示当前配置(例如方向)的最佳状态.

同样适用于使用Admob广告的新活动.如果之前加载,则无需重新加载.

编辑:我已经给了这个奖励,因为Google / Firebase支持通过电子邮件告诉我,实际上可以缓存广告:

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.

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.

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).

而且:

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.

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

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
07001
and
07002.
These are designed for exactly this kind of use case.

如果确实有可能,我想知道如何做到这一点.请成功执行此操作的人员,在代码中显示可以执行的操作.

解决方法

我认为使用智能横幅将是一个不错的选择,因为它们适用于任何方向
要使用智能横幅,请为广告尺寸指定常量SMART_BANNER:
AdView adView = new AdView(this);

adView.setAdSize(AdSize.SMART_BANNER);

还检查一下:
https://firebase.google.com/docs/admob/android/banner#smart_banners

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...