Savas Adar
Savas Adar

Reputation: 4262

admob InterstitialAd go into loop - android

i am using google admob InterstitialAd in my app, but i have a problem. When i install my app a mobile phone, it is running success. But when i install it a tablet device, InterstitialAd call activity onDestroy. When i close the ad, onCreate again being called. This continues in the form of a loop. how can i prevent go to onDestroy when ad shown?

on create:

// Create an ad.
interstitialAd = new InterstitialAd(this, "--------");

// Set the AdListener.
interstitialAd.setAdListener(this);
loadAd();

and methods:

private void loadAd(){
    AdRequest adRequest = new AdRequest();
    //adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
    interstitialAd.loadAd(adRequest);
}

@Override
public void onReceiveAd(Ad ad) {
    if (ad == interstitialAd) {
        if (interstitialAd.isReady()) {
            interstitialAd.show();
          }
    }
}

Upvotes: 1

Views: 810

Answers (1)

William
William

Reputation: 20196

Do NOT call interstitialAd.show() in AdListsner#onReceive.

This is at the core of your problem because you will get an ad loaded each time your Activity is created. And it will be created whenever you return to it after the Android framework has destroyed it to preserve memory. That can happen any time you leave an Activity such as when the interstitialAd Activity is displayed. It also generates an extremely poor UX.

Instead call interstitalAd.show() at a natural break point in your app, but only if interstitialAd.isReady().

Upvotes: 1

Related Questions