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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With