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?
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 whenonAdLoaded()is fired. However, foronAdLoaded()is fired, the interstitial must have been visibly displayed from before. So, since the interstitial is still around, it will dispatchonAdLoaded(), which shows the interstitial (showInterstitial()), which will dispatch anotheronAdLoaded(), which will callshowInterstitial()again and again.You need to send the ad request early, and utilize the
isLoaded()check before callingshowInterstitial().Send the ad request on app launch:
Then, put this in your
showInterstitial():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.