Skip to content Skip to sidebar Skip to footer

How To Pause Interstitial Ads When Android Application Goes To Background?

I have an issues with interstitial ad using admob for Android, which is the commercial video still running even I tap home button or goes to background. Is there anyone knows how t

Solution 1:

What version of the AdMob SDK are you using? This solution will work with Google Play Services.

Lets say that this is how you setup your interstitial:

//Here is where you setup your interstitial//...
adView.setAdListener(newAdListener() {
    @OverridepublicvoidonAdLoaded(){
        adView.show();
    }
});

In your onPause() method you can do the following:

protected void onPause() {
    super.onPause();

    if(adView != null) {
        adView.setAdListener(null);
    }
}

This works because by the time the onPause method is called because of a new interstitial, the ad will already be in the process of being shown and will display normally. However, if onPause() is called because of something else, like the Home button being pressed, it will disable new interstitials from being shown.

Only caveat is if you're using other callback methods available from the AdListener.

Solution 2:

Declare a global variable

booleanisActivityIsVisible=true;

Update variable base on Activity life cycles

@OverrideprotectedvoidonPause() {
    super.onPause();
isActivityIsVisible = false;
}

@OverrideprotectedvoidonResume() {
    super.onResume();
isActivityIsVisible = true;
}

Check whether Activity is visible in interstitial ads call back, If visible show it.

   mInterstitialAd.setAdListener(newAdListener() {
        @OverridepublicvoidonAdClosed() {
            requestNewInterstitial();
        }
        publicvoidonAdLoaded(){
            if (isActivityIsVisible) { mInterstitialAd.show(); }
        }
    });

Post a Comment for "How To Pause Interstitial Ads When Android Application Goes To Background?"