Adding AdMob interstitial ads before closing the app

1.4k views Asked by At

I want to display AdMob interstitial ad on exit link from the nav bar and close the app on click of either on the interstitial ad or the close button of the ad.

I am using the following codes but it won't work:

InterstitialAd mInterstitialAd;
    void loadAdsFullScreen(){
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad));
        AdRequest request = new AdRequest.Builder()
                .tagForChildDirectedTreatment(true)
                .build();
        // Load ads into Interstitial Ads
        mInterstitialAd.loadAd(request);

        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                showInterstitial1();
            }
        });
    }

The code I am using for closing the app and showing the ads is:

else if (id == R.id.nav_exit) {

            mInterstitialAd = new InterstitialAd(this);
            mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad));
            AdRequest request = new AdRequest.Builder()
                    .tagForChildDirectedTreatment(true)
                    .build();
            // Load ads into Interstitial Ads
            mInterstitialAd.loadAd(request);

            mInterstitialAd.setAdListener(new AdListener() {
                public void onAdLoaded() {
                    showInterstitial();
                    finish();
                }
            });

            Test1.this.finish();
            System.exit(0);

        }
2

There are 2 answers

8
Exigente05 On BEST ANSWER

Override onAdClosed of listener like,

mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                showInterstitial();

            }
             @Override
            public void onAdClosed() {
            Activity_Class_Name.this.finish();
        }

        @Override
        public void onAdOpened() {
            Activity_Class_Name.this.finish();
        }

        });
0
Yasser Benmman On

I don't encourage you to do interstitial ads when exiting the app since google Admob privacy policy doesn't allow that.