Search code examples
androidadmobinterstitial

Admob interstitial on app start


i want to have an admob interstitial ad show up when the user first opens the app or when the user navigates to another app (like browser) and then returns to my app. This is my current code for interstital ad, this code is entirely contained inside the OnCreate method.

// Create the interstitial.
        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("ca-app-pub-XXXXXXXXXXXXXXXX");

        // Create ad request.
        AdRequest adRequest2 = new AdRequest.Builder().build();

        // Begin loading your interstitial.
        interstitial.loadAd(adRequest2);

        interstitial.setAdListener(new AdListener() {

            @Override
            public void onAdLoaded() {
                if (interstitial.isLoaded()) {
                    interstitial.show();
                }

            }

        });

This seems to work for most situations, but on certain devices this will create a loop of interstitial showing 2-3 sec after they are dismissed by the user. one of those device that has the loop is a Galaxy Tab3 . i cant seem to figure out a proper way to setup my code so that this behavior does not happen on any device.


Solution

  • Create a boolean var to assist with it..

    boolean ad_shown = false;
    

    When you do .show() turn the variable to true.

    Don't forget to put a guard on the if

    if (interstitial.isLoaded() && !ad_shown) {