9

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

blackkara
  • 4,900
  • 4
  • 28
  • 58

1 Answers1

5

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.

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • Unfortunately nothing is new for me. The `StackOverflowException` can be occurred by network case in `onError` callback of all `NativeAd`, `NativeAdsManager`, `InterstitialAd` objects when it doesn't manage properly, so not only for `NativeBannerAd`. Am i wrong? – blackkara Jan 30 '20 at 08:33
  • @blackkara considering the best practices, you need to first check if the network is available or you can implement a reactive or event based solution to show the ad. creating a new ad from on error if just giving it another try(where another add won't try to instantiate new ad in case of onError). – Pavneet_Singh Jan 30 '20 at 10:59