Question

This question is asked more, but there are different solutions given and I honestly don't know what the best practice is here.

I've got an interstitial ad like this (Google Admob SDK):

private InterstitialAd interstitial;

However the ad is delayed a bit and if a user quickly navigates (within a second or two), then the ads shows at a different screen than I would like, or even worse when the application is already in the background.

How to pause interstitial Ads when Android application goes to background? shows a function that doesn't exist (anymore).
When dismiss interstitial DFP ad on Android, the app crashes uses a different ad apparently than I do... is this better to use?
programmatically close an Interstitial ad simply says it's impossible to cancel it.

What i preferably would like it to just cancel the ad in the onDestroy() function and pause (or destroy aswell) the ad in the onPause() function.

Is this possible to do? Any workaround or what not is also fine, but I of course prefer a clean solution.

Was it helpful?

Solution 2

You should really have shown some code, but from your symptoms I suspect you are showing your Interstitial from the #onAdReceived method of an AdListener. DO NOT DO THIS

You should be showing your Interstitial at a natural break point in your app instead. Otherwise you will get the behaviour described as well as provide an extremely poor user experience.

OTHER TIPS

As William pointed out, you should not show it in onAdReceived. What I do is set a boolean flag in onAdReceived, then I have a background thread do an empty while loop (with a timer) and check the boolean before calling show().

I still call isLoaded(), but at that point I do know it's loaded. The reason why I don't just use isLoaded() by itself is because it has to run on the UI thread and I don't want to block the UI while I wait for the ad to load.

Try this to invalidate showing ads when the activity finished:

@Override
public void onAdClosed() {
    mInterstitialAd = null;
}

@Override
public void onAdLoaded() {
    if (mInterstitialAd != null)
        mInterstitialAd.show();
    mInterstitialAd = null;
}

@Override
public void onAdFailedToLoad(int errorCode) {
    mInterstitialAd = null;
}

onDestroy / onStop() {
    mIntertitial = null;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top