Interstitial Ads

Interstitial ads provide full-screen experiences, commonly incorporating rich media to offer a higher level of interactivity compared to banner ads. Interstitials are typically shown during natural transitions in your app; for example, after completing a game level, or while waiting for a new view to load. Use the MoPubInterstitial object and its associated listeners to fetch and display interstitial ads in your app.

Prerequisites

Go through the steps in our Getting Started Guide to create an account on MoPub and integrate the SDK into your project. It is essential that you added the necessary Activities to your AndroidManifest.xml file as instructed.

Add any other activities as required by mediated SDKs you are adding. For additional details, check the respective network guides.

Create an ad unit in the Publisher UI. You will need that ad unit’s ID.

Load Interstitial Ads in Your App

To be safe, once again check your AndroidManifest.xml file to make sure you included the necessary custom Activities we discussed in the Prerequisites section (even though you won’t instantiate them directly).

Step 1. Create an Interstitial Ad

The MoPub SDK provides a custom class, MoPubInterstitial, that handles fetching and displaying fullscreen interstitial ads. To ensure a smooth experience, you should pre-fetch the content as soon as your Activity is created, then display it if the fetch was successful.

In the Activity in which you want to show the interstitial ad, declare a MoPubInterstitial instance variable:

// Remember that "this" refers to your current activity.mInterstitial.setInterstitialAdListener(this);

Step 2. Display an Interstitial Ad

Now you’re ready to display an ad:

In your onCreate() method, call load() to begin caching the ad. It’s important to fetch interstitial ad content well before you plan to show it, because it often incorporates rich media and may take some time to load. We suggest caching when your Activity is first created, but you may also choose to do it based on events in your app, such as the start of a game level.

Note: The load() method will take a few seconds as it requests and caches ad content – including javascript, images, and additional resources. As a best practice, provide sufficient time (by waiting for the appropriate InterstitialAdListener callbacks) before calling show().

When you’re ready to display your ad, use InterstitialAdListener#onInterstitialLoaded() callback and the MoPubInterstitial.isReady() to check whether the interstitial was successfully cached.

If isReady() returns true, display the interstitial by calling the show() method. You can add logic to the other interstitial lifecycle methods to handle what should happen when your user interacts with the ad; for example, resuming your game on onInterstitialDismissed().

Lastly, remember to call destroy() on the interstitial in your Activity’s onDestroy method.

Your completed Activity code should look something like this (see InterstitialDetailFragment.java from mopub-sample for a full example):

publicclassExampleActivityextendsActivityimplementsInterstitialAdListener{MoPubInterstitialmInterstitial;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);mInterstitial=newMoPubInterstitial(this,YOUR_INTERSTITIAL_AD_UNIT_ID_HERE);mInterstitial.setInterstitialAdListener(this);mInterstitial.load();}@OverrideprotectedvoidonDestroy(){mInterstitial.destroy();super.onDestroy();}// Defined by your application, indicating that you're ready to show an interstitial ad.voidyourAppsShowInterstitialMethod(){if(mInterstitial.isReady()){mInterstitial.show();}else{// Caching is likely already in progress if `isReady()` is false.// Avoid calling `load()` here and instead rely on the callbacks as suggested below.}}// InterstitialAdListener methods@OverridepublicvoidonInterstitialLoaded(MoPubInterstitialinterstitial){// The interstitial has been cached and is ready to be shown.}@OverridepublicvoidonInterstitialFailed(MoPubInterstitialinterstitial,MoPubErrorCodeerrorCode){// The interstitial has failed to load. Inspect errorCode for additional information.}@OverridepublicvoidonInterstitialShown(MoPubInterstitialinterstitial){// The interstitial has been shown. Pause / save state accordingly.}@OverridepublicvoidonInterstitialClicked(MoPubInterstitialinterstitial){}@OverridepublicvoidonInterstitialDismissed(MoPubInterstitialinterstitial){// The interstitial has being dismissed. Resume / load state accordingly.}}

Sample Code

For a complete example of how to add interstitial ads into your app, check out the mopub-sample.

Best Practices

Mediation SDKs might rely on select Activity lifecycle events for tracking purposes. For the SDKs that do, you are required to notify MoPub of when those events happen, so that MoPub can pass them onto mediated networks, like so: