Search code examples
javaandroidadmobinterstitial

How to add limit ad load retries inside onAdFailedToLoad() in Admob Ads?


Within the guide on how to place Admob interstitial ads in the app, you have the following warning:

Warning: Attempting to load a new ad from the onAdFailedToLoad () method is strongly discouraged. If you must load an ad from onAdFailedToLoad (), limit ad load retries to avoid continuously failed ad requests in situations such as limited network connectivity.

I was putting a

mInterstitialAd.loadAd (new AdRequest.Builder (). build ());

inside the onAdFailedToLoad () and it seems that this is not the correct one. What is the best practice for doing this type of limit within the onAdFailedToLoad ()?


Solution

  • Maybe create the maximum number of new requests?

    const val MAXIMUM_NUMBER_OF_AD_REQUEST = 5
    
    class MainActivity : AppCompatActivity()
    {
        private var loadAdInterstitialRequests = 0
    }
    

    And then in AdListener:

    override fun onAdFailedToLoad(p0: Int)
    {
        super.onAdFailedToLoad(p0)
        if (loadAdInterstitialRequests++ < MAXIMUM_NUMBER_OF_AD_REQUEST)
        {
            mInterstitialAd.loadAd(AdRequest.Builder().build())
        }
    }
    

    This will limit sending new add requests to MAXIMUM_NUMBER_OF_AD_REQUEST.