Search code examples
androidadsinterstitial

Interstitial ad displaying while app is in background


I have a interstitial ad set up to show after 5 minutes, however I do not want it to open unless the user is viewing the app. There is a issue in testing where if you go to another app, it will still pop up over it all.


Solution

  • When you leave the application (home button, etc), you need to clear the AdListener in onPause() callback.

    Assuming that you've set the adListener in your adView, you may set his listener to null when android call onPause. It would be something like this:

    protected void onPause() {
        super.onPause();
    
        if(adView != null) {
            adView.setAdListener(null);
        }
    }
    

    I don't know what version of admob you're using, but this should work with Google Play Services.