我知道可以通过清单为活动设置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