How to pause interstitial Ads when Android application goes to background?

て烟熏妆下的殇ゞ 提交于 2019-11-29 11:41:12

What version of the AdMob SDK are you using? This solution will work with Google Play Services.

Lets say that this is how you setup your interstitial:

//Here is where you setup your interstitial
//...
adView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded(){
        adView.show();
    }
});

In your onPause() method you can do the following:

protected void onPause() {
    super.onPause();

    if(adView != null) {
        adView.setAdListener(null);
    }
}

This works because by the time the onPause method is called because of a new interstitial, the ad will already be in the process of being shown and will display normally. However, if onPause() is called because of something else, like the Home button being pressed, it will disable new interstitials from being shown.

Only caveat is if you're using other callback methods available from the AdListener.

Declare a global variable

boolean isActivityIsVisible = true;

Update variable base on Activity life cycles

 @Override
protected void onPause() {
    super.onPause();
isActivityIsVisible = false;
}

@Override
protected void onResume() {
    super.onResume();
isActivityIsVisible = true;
}

Check whether Activity is visible in interstitial ads call back, If visible show it.

   mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            requestNewInterstitial();
        }
        public void onAdLoaded(){
            if (isActivityIsVisible) { mInterstitialAd.show(); }
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!