user2580712
user2580712

Reputation:

How to cache Interstitial ads to show them at app exit?

I have a stable 15k downloads app on the play store, looking to expand monetization... I want to add interstitial ads.

I already have banner ads that are working pretty decently...

The problem I'm having with the interstitial ads is:

I want to show these ads when the user presses back button at the main menu, that is when the app closes I want to show the full screen ad. (Interstitial or House ad, whatever it is called - The fullscreen ones are what I'm talking about.)

https://developers.google.com/mobile-ads-sdk/docs/admob/advanced

I've tried basic text book code for doing this, but I'm not really sure how to. The problem is the app closes before it has a chance to show the ad.

Here's the code:

import com.google.ads.*;

public class BannerExample extends Activity implements AdListener {

  private InterstitialAd interstitial;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Create the interstitial
    interstitial = new InterstitialAd(this, MY_INTERSTITIAL_UNIT_ID);

    // Create ad request
    AdRequest adRequest = new AdRequest();

    // Begin loading your interstitial
    interstitial.loadAd(adRequest);

    // Set Ad Listener to use the callbacks below
    interstitial.setAdListener(this);
  }

  @Override
  public void onReceiveAd(Ad ad) {
    Log.d("OK", "Received ad");
    if (ad == interstitial) {
      interstitial.show();
    }
  }
}

The only modification I've done is I've tried putting it in the OnDestroy(). But the app closes before the ad loads and shows and is closed or pressed.

As it is, the interstitial ad does show up. If I simply copy paste the above code in the OnCreate of my Main menu activity, the ad may or may not show up at that activity. It shows up later on, even when I navigate away from that screen.

Again, the real problem: I'm trying to show the interstitial ad when the app closes. So I need some way to load it before the user tries to exit. My app is pretty light, Plus it's a text based app, so there might be a lot of going back and forth from the menu in which I want to show the ad (the main menu). So loading it every time that activity opens up might create performance issues.

I want to load the ad when the app starts, and keep it somewhere safely and when the app closes, make it pop up.

So, How to do this?

Upvotes: 2

Views: 7158

Answers (3)

Krzysztof Cieśliński
Krzysztof Cieśliński

Reputation: 339

You can't finish activity after showing interstitial because it won't show up.

Also it's not a good idea to put interstitial.show() in onDestroy() method.

I'm surprised that there is still no proper and complete answer for this question so I will put my code which is working properly.

public class MainActivity extends Activity {

// AdMob full screen ad
private InterstitialAd interstitial;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);

    // AdMob full screen ad
    AdRequest adFullScreenRequest = new AdRequest.Builder().build();
    interstitial = new InterstitialAd(this);
    interstitial.setAdUnitId("myAdUnitId");
    interstitial.loadAd(adFullScreenRequest);
    interstitial.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            super.onAdClosed();
            finish();
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            super.onAdFailedToLoad(errorCode);
            finish();
        }
    });
}

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

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch(keyCode){
        case KeyEvent.KEYCODE_BACK:
            displayInterstitial();
            return true;
    }
    return super.onKeyDown(keyCode, event);
}

}

Important thing is to set AdListener for two situations:

  • User closed ad

  • Ad failed to load

In those situations you must close activity (and ad) by finish() method.

Another thing is to use finish() if our interstitial is not loaded yet. If you won't use finish() there then backButton won't work at all until interstitial will be loaded. It takes some time to load interstitial (about few seconds). There are other options how to solve this but here is my solution which is simply close the app if interstitial is not loaded yet.

Upvotes: 4

Mr T
Mr T

Reputation: 1509

@Override
public void onBackPressed() {
   Log.d(TAG, "onBackPressed Called");
       if (interstitial.isLoaded()) {
         interstitial.show();
        }

   finish();

}

Upvotes: 0

Rabi
Rabi

Reputation: 2593

You can try showing the interstitial only after the user presses the back button:

    @Override
    public void onBackPressed() {
        if (interstitial != null && interstitial.isReady()) {
            interstitial.show();
        }
        super.onBackPressed();
    }

Upvotes: 4

Related Questions