Я пишу нативную рекламу для admob. Я использую только один экземпляр для всего моего приложения. Вот мой код:
private WeakReference<UnifiedNativeAdView> NATIVEAD;
private UnifiedNativeAd nativeAd;
protected void loadNaAd(final ViewGroup view, String adTypes) {
WeakReference<ViewGroup> llBannerContainer = new WeakReference<>(view);
MySetting setup = MySetting.getSetting();
if (view != null && setup != null && !TextUtils.isEmpty(BuildConfig.DEBUG ? "ca-app-pub-3940256099942544/2247696110" : setup.ads.ids.admob.n)) {
if (NATIVEAD == null) {
NATIVEAD = new WeakReference<>((UnifiedNativeAdView) LayoutInflater.from(Utils.getApp()).inflate(R.layout.ad_unified, null));
new AdLoader.Builder(view.getContext(), BuildConfig.DEBUG ? "ca-app-pub-3940256099942544/2247696110" : setup.ads.ids.admob.n)
.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
@Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
if (nativeAd != null) {
nativeAd.destroy();
}
nativeAd = unifiedNativeAd;
if (NATIVEAD.get() == null) {
NATIVEAD = new WeakReference<>((UnifiedNativeAdView) LayoutInflater.from(Utils.getApp()).inflate(R.layout.ad_unified, null));
}
populateUnifiedNativeAdView(unifiedNativeAd, NATIVEAD.get());
AdsManager.addNativeToView(llBannerContainer.get(), NATIVEAD.get());
}
})
.withAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
UnifiedNativeAdView mAdView = NATIVEAD.get();
if (mAdView != null) {
mAdView.destroy();
mAdView = null;
}
if (nativeAd != null) {
nativeAd.destroy();
nativeAd = null;
}
AdsManager.loadNaAds(llBannerContainer.get(), AdsManager.getNext(AdsManager.ADMOB, adTypes));
}
})
.build()
.loadAd(new AdRequest.Builder().build());
}
if (nativeAd != null) {
AdsManager.addNativeToView(view, NATIVEAD.get());
}
}
}
Почему это происходит, когда я использовал слабые ссылки для просмотра?