Skip to content Skip to sidebar Skip to footer

How To Automatically Close Admob Interstitial Ad If Users Click The Android Home Button?

I was using the interstitial ad from Admob in my android app. The interstitialAd.show() is called at certain events of the app. Everything worked fine but one annoying use case wit

Solution 1:

If anyone is looking for an answer to this. If the user hits the Home button while the interstitial ad is being shown, this prevents it from showing when the user launches the app again.

In your AndroidMainifest.xml file add: android:noHistory="true" to the AdActivity activity

Like this:

<activity android:name="com.google.android.gms.ads.AdActivity"
   android:noHistory="true"
 android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
</activity>

This seems to be working so far.

Solution 2:

This may be too late but you cannot close the ad automatically. A hack around it is that in your onBackPressed() method, after the user taps on the back button to close the ad, you return the user to the application and using a Toast method, alert the user to click the back button again to exit the application. This approach also satisfies Google's suggestion that you allow the user to know which app is displaying the ads, thus you return to the app before closing it finally. In terms of the user experience, it may not be the best but it sure does do the trick. Below is a sample code.

boolean doubleBackToExitPressedOnce = false;
publicvoidonBackPressed() {
    if(doubleBackToExitPressedOnce) {
       super.onBackPressed();
       return;
    }

    if(backPressAd.isLoaded()) {
        backPressAd.show();
        backPressAd.setAdListener(newAdListener(){
           @OverridepublicvoidonAdClosed() {
              doubleBackToExitPressedOnce = true;
              Toast.makeText(getApplicationContext(), "Click BACK again to exit", Toast.LENGTH_LONG).show();
              backPressAd.loadAd(newAdRequest.Builder().build());
           }
        });
    } else {
      finish();
    }
}

Solution 3:

I don't think there is a solution.

Essentially the Ad.show() needs to open up the ad in a new Task so it is not part of the Activity list for the current Task/app.

Post a Comment for "How To Automatically Close Admob Interstitial Ad If Users Click The Android Home Button?"