0

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.

Zaid90
  • 81
  • 1
  • 11

2 Answers2

3

Use the following code to display the interstitial ad when appstarts.

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("***********");

AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener() {
    public void onAdLoaded() {
        displayInterstitial();
    }
});
}

public void displayInterstitial() {
if (interstitial.isLoaded()) {
    interstitial.show();
}
}
arunkumar
  • 67
  • 2
  • 10
1

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) {
noidraug
  • 219
  • 1
  • 9