0
votes

I want to load AdMob interstitial ads when the users opens the app and resumes the app after minimizing the app.

I am using the following code to load AdMob interstitial ads onResume:

@Override
protected void onResume() {
    super.onResume();
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad));
    AdRequest request = new AdRequest.Builder()
            .tagForChildDirectedTreatment(true)
            .build();
    mInterstitialAd.loadAd(request);

mInterstitialAd.setAdListener(new AdListener() {
    public void onAdLoaded() {
        showInterstitial();
    }
});
}

But the ads keep loading repeatedly after closing. I tried to limit ads display once in 5 minutes in AdMob settings but it didn't work. How do I prevent ads from loading repeatedly?

1

1 Answers

2
votes

I want to load AdMob interstitial ads when the users opens the app and resumes the app after minimizing the app.

This is prohibited as per interstitial best practices: https://support.google.com/admob/answer/6201362?hl=en&ref_topic=2745287.

Your code creates a circuit. You are loading the interstitial on the activity's onResume(), and showing it when onAdLoaded() is fired. However, for onAdLoaded() is fired, the interstitial must have been visibly displayed from before. So, since the interstitial is still around, it will dispatch onAdLoaded(), which shows the interstitial (showInterstitial()), which will dispatch another onAdLoaded(), which will call showInterstitial() again and again.

You need to send the ad request early, and utilize the isLoaded() check before calling showInterstitial().

Send the ad request on app launch:

// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");

// Create the InterstitialAd and set the adUnitId.
mInterstitialAd = new InterstitialAd(this);

// Defined in res/values/strings.xml
mInterstitialAd.setAdUnitId(getString(R.string.ad_unit_id));

if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
        AdRequest adRequest = new AdRequest.Builder().build();
        mInterstitialAd.loadAd(adRequest);
}

Then, put this in your showInterstitial():

if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
}

EDIT: Only call showInterstitial() at a logical breakpoint in your app. Showing interstitial ads on app launches or app exits does not comply with AdMob's interstitial best practices.