Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do We Call loadAd Method of NativeBannerAd More Than Once?

I'm just trying to manage fail scenarios of ads in my app. So, after onError triggered due to some reason(e.g no network) I just called loadAd method of NativeBannerAd like below. But app crashed with called more than once error.

 mFacebookNativeBannerAd = NativeBannerAd(context, id)
 val builder = mFacebookNativeBannerAd.buildLoadAdConfig()
 builder.withAdListener(object : NativeAdListener {
     // ...
     // Other callback methods
     override fun onError(p0: Ad?, error: AdError?) {
         mFacebookNativeBannerAd.loadAd() // --> 'called more than once' exception
     }
 })
 mFacebookNativeBannerAd.loadAd()

I didn't see any description about this exception on documents. NativeAd, NativeAdsManager, InterstitialAd objects works with above scenario. But NativeBannerAd does not.

How can i load the ad again ?

The audience version i'm using is audience-network-sdk:5.6.1

like image 686
blackkara Avatar asked Jan 26 '20 01:01

blackkara


People also ask

What is facebook Native ads?

Add Native Banner Ads to an Android App. The native banner ad API allows you to build a customized experience for showing a native ad without the advertiser's creative assets, such as image, video, or carousel content.


1 Answers

Once the Facebook NativeBannerAd is failed then a new banner object needs to be created for every reload because the same object cannot be used again so you can create a method which will instantiate a new NativeBannerAd object and load it.

You must be thinking Why not reuse the same object from onError?

Because it's a code smell. In case of network error(your use case), the add will keeps on trying to load it self(can add retry logic but still code smell) and eventually will crash your app with StackOverflowException due to recursive behaviour.

Documentation reference as POC

Ad Instance is not an ad manager. You are supposed to instantiate a new instance whenever you need "reload" an ad for native ads and banner ads.

like image 86
Pavneet_Singh Avatar answered Nov 07 '22 00:11

Pavneet_Singh