Can we reuse the same NativeContentAd multiple times in a RecyclerView?

801 views
Skip to first unread message

Fran Soto

unread,
Jun 26, 2018, 3:43:05 AM6/26/18
to Google Mobile Ads SDK Developers
I want to show some ads inside a recyclerview but as loadAd is pretty intensive in terms of RAM and CPU usage i want to load a few and reuse it in the recyclerview.

Something like

MyAdapter: Adapter() {
...

fun onBindViewHolder(holder: ViewHolder) {
    ...
    if (holder is AdViewHolder) {
         if (ads.size > 5) {
            holder.bind(randomAdsFromList(ads))
         } else {
            loadAdCacheItAndBind { nativeAd -> holder.bind(nativeAd) }
         }
    }
}

Is this approach correct ?

Can i show the same NativeContentAd multiple times ?


mobileadssdk-a...@google.com

unread,
Jun 26, 2018, 3:41:18 PM6/26/18
to Fran Soto, Google Mobile Ads SDK Developers
Hi Fran,

Thanks for reaching out to us. You can cache the response from the very first Ad Request and show it all the time if you want to. You can even prefetch multiple ads with the same AdRequest using the loadAds() method. Please note that if you are prefetching the ads and trying to display it later, it's important that you should not keep old ads around too long without displaying them. Any ad objects that have been held for longer than an hour without being displayed should be discarded and replaced with new ads from a new request. If you've cached an ad for 30 minutes and still haven't shown it, we recommend loading a fresh one.  
 
Let us know if that helps. 

Regards,
Deepika Uragayala
Mobile Ads SDK Team
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and Google+ page:
    http://googleadsdeveloper.blogspot.com
    https://plus.google.com/115658573333388777174/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

--

---
You received this message because you are subscribed to the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-admob-ads-sdk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-admob-ads-sdk/c7d4b198-20a9-4584-9e53-b3ee097452cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Fran Soto

unread,
Jun 27, 2018, 7:18:38 AM6/27/18
to Google Mobile Ads SDK Developers
So to be sure, 

If i shown an ad in my recycler view each 10 items. I can request 1 ad and show it at positions 10, 30, 50, ... and fetch another ad and show it at  positions 20, 40, 60, ...

Hi Fran,

Thanks for reaching out to us. You can cache the response from the very first Ad Request and show it all the time if you want to. You can even prefetch multiple ads with the same AdRequest using the loadAds() method. Please note that if you are prefetching the ads and trying to display it later, it's important that you should not keep old ads around too long without displaying them. Any ad objects that have been held for longer than an hour without being displayed should be discarded and replaced with new ads from a new request. If you've cached an ad for 30 minutes and still haven't shown it, we recommend loading a fresh one.  
 
Let us know if that helps. 

Regards,
Deepika Uragayala
Mobile Ads SDK Team
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

mobileadssdk-a...@google.com

unread,
Jun 27, 2018, 3:35:13 PM6/27/18
to Fran Soto, Google Mobile Ads SDK Developers
Hi Fran,

Yes you can certainly do that. Try to reuse the ad if it's not shown to the user. Or make another ad request by prefetching it and then only showing it when needed. Let us know if you are finding any specific issues with your implementation and provide us a sample app if you need further support.

Regards,
Deepika Uragayala
Mobile Ads SDK Team
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and Google+ page:
    http://googleadsdeveloper.blogspot.com
    https://plus.google.com/115658573333388777174/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

So to be sure, 

To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

--

---
You received this message because you are subscribed to the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-admob-ads-sdk@googlegroups.com.

Raunak Yadav

unread,
May 4, 2020, 2:26:43 AM5/4/20
to Google Mobile Ads SDK Developers
Hi Google team,

I had a doubt on ad reuse scenario. I have a similar scenario reported in the question here. Now I am facing a crash in field which looks because of reuse.
I have a viewPager that contains different lists. I insert the ad in first list and if it is not seen and user moves to 2nd list, I reuse the same ad there. I think there should be some call to unregister the nativeAd before it can be used. 

Fatal Exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
       at android.view.ViewGroup.addViewInner(ViewGroup.java:5269)
       at android.view.ViewGroup.addView(ViewGroup.java:5090)
       at android.view.ViewGroup.addView(ViewGroup.java:5030)
       at android.view.ViewGroup.addView(ViewGroup.java:5003)
       at com.google.android.gms.ads.internal.formats.ah.a(ah.java:44)
       at com.google.android.gms.ads.internal.formats.client.k.a(k.java:25)
       at fa.onTransact(fa.java:4)
       at android.os.Binder.transact(Binder.java:675)
       at com.google.android.gms.internal.ads.zzfn.zza(zzfn.java:20)
       at com.google.android.gms.internal.ads.zzabo.zze(zzabo.java:16)
       at com.google.android.gms.ads.formats.NativeAdView.setNativeAd(NativeAdView.java:35)

Please help.

Mobile Ads SDK Forum Advisor Prod

unread,
May 4, 2020, 4:20:07 AM5/4/20
to 123.r...@gmail.com, google-adm...@googlegroups.com

Hi Raunak,

 

Thank you for reaching out to us.

 

First off, kindly note that NativeContentAd has already been deprecated in favor of UnifiedNativeAd. I would advise using the latter instead going forward to prevent possible issues in the future; you may refer to this page to get started with UnifiedNativeAd objects.

 

Going back to your original concern: the way to reuse native ads inside a RecyclerView may look different from just registering and unregistering ad views from the list. You may refer to this guide as an example on how to implement native ads in a RecyclerView. Note that it already uses the UnifiedNativeAd object as mentioned above; you may want to get acquainted with this object first before trying it out in the RecyclerView.

 

Regards,

Ziv Yves Sanchez

Mobile Ads SDK Team



ref:_00D1U1174p._5004Q1zJwUH:ref

Raunak Yadav

unread,
May 4, 2020, 6:17:33 AM5/4/20
to Google Mobile Ads SDK Developers
Hi.
Thanks for the quick reply.

I am adding the ads to recyclerview in exact same way as demonstrated in the guide referred by you. Still in some cases, we are getting the mentioned crash in production. However I am not getting the crash on my phone. I feel it happens in some scenario only. Not sure why. Also, I am using Google Ad Manager instead of Admob. Would that make a difference here? Please point me to the correct forum if it does. 
Update to Unified ad apis is in pipeline. We are working on it for our next release.

Mobile Ads SDK Forum Advisor Prod

unread,
May 4, 2020, 10:53:39 AM5/4/20
to 123.r...@gmail.com, google-adm...@googlegroups.com

Hi Raunak,

I am on Ziv's team. You have come to the correct forum. Implementing the Mobile Ads SDK for AdMob is mostly the same as implementing the SDK for AdManager. For Native Ads I have seen advice that to be certain that the ad can be reused in another frame to call removeAllViews() on the old FrameLayout holding the UnifiedNativeAdView. It's possible that the ad is bound and waiting to be displayed in the first RecyclerView if it's the next in the list to show. A way to test for that is to turn off your logic to reuse only if not seen and see if you get the error when switching viewPagers after seeing an ad. There may be other anomalies that may cause this. If you can replicate the error can you kindly post an app here based on our sample Native app  with steps to take to replicate this error?

Regards,
Aryeh Baker
Mobile Ads SDK Team



ref:_00D1U1174p._5004Q1zJwUH:ref

Arvind Chakravarthy

unread,
May 27, 2020, 7:55:29 AM5/27/20
to Google Mobile Ads SDK Developers
Hi Team, 

I too have the same issue in production. And not able to reproduce it in my device. However I am not reusing the same ad if not seen. And I call the removeAllViews() from the parent view before rendering the native ad.   

Fatal Exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
       at android.view.ViewGroup.addViewInner(ViewGroup.java:5099)
       at android.view.ViewGroup.addView(ViewGroup.java:4930)
       at android.view.ViewGroup.addView(ViewGroup.java:4870)
       at android.view.ViewGroup.addView(ViewGroup.java:4843)
       at com.google.android.gms.ads.internal.formats.ah.a(:com.google.android.gms.policy_ads_fdr_dynamite@21460004@21460004.297791526.297791526:44)
       at com.google.android.gms.ads.internal.formats.client.k.a(:com.google.android.gms.policy_ads_fdr_dynamite@21460004@21460004.297791526.297791526:25)
       at fa.onTransact(:com.google.android.gms.policy_ads_fdr_dynamite@21460004@21460004.297791526.297791526:4)
       at android.os.Binder.transact(Binder.java:675)
       at com.google.android.gms.internal.ads.tx1.b(com.google.android.gms:play-services-ads-base@@18.3.0:2)
       at com.google.android.gms.internal.ads.q1.d(com.google.android.gms:play-services-ads-lite@@18.3.0:3)
       at com.google.android.gms.ads.formats.UnifiedNativeAdView.setNativeAd(com.google.android.gms:play-services-ads-lite@@18.3.0:1)

Regards
Arvind

Mobile Ads SDK Forum Advisor Prod

unread,
May 27, 2020, 9:09:07 AM5/27/20
to arv...@gmail.com, google-adm...@googlegroups.com
Hi Arvind,

Thank you for bringing this already bound Native Ad issue to us. Could you elaborate on what the use case is - ie do you have multiple activities in your app or ViewPagers or RecyclerViews or a combination of the above? Do you manually unbind from a RecyclerView ads that were about to be seen? Is this crash recorded on older phones or also newer models?

Kind regards,
Reply all
Reply to author
Forward
0 new messages