James Nguyen
James Nguyen

Reputation: 71

How to show Admob interstitial on app start and exit

I can make to show Admob Interstitial on App start, I also can make to show Admob Interstitial on App exit. But now, I want to show Admob Interstitial when App start AND exit. I have tried to combine 2 codes, one for the app start and one for the app exit, but result only show ads on app start, the ad does not appear on app exit.

Here my code:

    InterstitialAd interstitial;
private static final String AD_UNIT_ID = "ca-app-pub-2869508995487312/2690564381";
private InterstitialAd interstitialAd;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.science_layout);

    interstitialAd = new InterstitialAd(this);

    interstitialAd.setAdUnitId(AD_UNIT_ID);
    AdRequest adRequest = new AdRequest.Builder().build();

    interstitialAd.loadAd(adRequest);

    interstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {

                if (interstitialAd.isLoaded()) {
                    interstitialAd.show();
                }

        }

        @Override
        public void onAdOpened() {


        }

        @Override
        public void onAdFailedToLoad(int errorCode) {

        }
    });
}
@Override
protected void onPostResume() {
    // TODO Auto-generated method stub
    super.onPostResume();
    displayInterstitial();
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    displayInterstitial();
}

// Invoke displayInterstitial() when you are ready to display an interstitial.
    public void displayInterstitial() {
      if (interstitialAd.isLoaded()) {
        interstitialAd.show();
      }
    }

Anyone can help me fix code or provide new solutions? Thanks & Best regards !

Upvotes: 0

Views: 4691

Answers (2)

Aegon
Aegon

Reputation: 125

It's against Admob rules however i advice showing ad after splashscreen as google advices Admob Interstitial after app loads

Upvotes: 0

Akshit Rewari
Akshit Rewari

Reputation: 941

Google's guidelines for interstitial ads clearly says that it is not recommended to show ads on app load or exit.

https://support.google.com/admob/answer/6066980?hl=en

> App load or exit (not recommended)

Avoid placing interstitial ads on app load, back buttons, and when exiting apps as interstitials are meant to be placed in between content.

Coming to your code, it should be working fine for app load time. Secondly , once an ad is loaded and displayed , you have to load it again to show it again. For calling it you should write "displayInterstitial();" in the onPause instead of onStop and only in one activity of the app.

http://developer.android.com/training/basics/activity-lifecycle/pausing.html

However , I don't think you should show ads on app load and exit as it can cause issues with your admob account and will increase the uninstall rate of your app.

Upvotes: 3

Related Questions