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.xmlfile 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).
After that, loading interstitial ads is a two-step process:
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
Activityin which you want to show the interstitial ad, declare aMoPubInterstitialinstance variable:private MoPubInterstitial mInterstitial; -
MoPubInterstitialprovides a listener interface,InterstitialAdListener, which you’ll use to stay informed about ad lifecycle events. Make sure yourActivityimplements theInterstitialAdListenerinterface. -
In your
Activity’sonCreate()method, instantiate theMoPubInterstitialusing the context and the Ad Unit ID you created in the Getting Started.mInterstitial = new MoPubInterstitial(this, YOUR_INTERSTITIAL_AD_UNIT_ID_HERE); -
Next, register your activity as the
InterstitialAdListener:// 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, callload()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 yourActivityis 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 appropriateInterstitialAdListenercallbacks) before callingshow(). -
When you’re ready to display your ad, use
InterstitialAdListener#onInterstitialLoaded()callback and theMoPubInterstitial.isReady()to check whether the interstitial was successfully cached. -
If
isReady()returns true, display the interstitial by calling theshow()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 ononInterstitialDismissed(). -
Lastly, remember to call
destroy()on the interstitial in yourActivity’sonDestroymethod. -
Your completed
Activitycode should look something like this (seeInterstitialDetailFragment.javafrom mopub-sample for a full example):public class ExampleActivity extends Activity implements InterstitialAdListener { MoPubInterstitial mInterstitial; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mInterstitial = new MoPubInterstitial(this, YOUR_INTERSTITIAL_AD_UNIT_ID_HERE); mInterstitial.setInterstitialAdListener(this); mInterstitial.load(); } @Override protected void onDestroy() { mInterstitial.destroy(); super.onDestroy(); } // Defined by your application, indicating that you're ready to show an interstitial ad. void yourAppsShowInterstitialMethod() { 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 @Override public void onInterstitialLoaded(MoPubInterstitial interstitial) { // The interstitial has been cached and is ready to be shown. } @Override public void onInterstitialFailed(MoPubInterstitial interstitial, MoPubErrorCode errorCode) { // The interstitial has failed to load. Inspect errorCode for additional information. } @Override public void onInterstitialShown(MoPubInterstitial interstitial) { // The interstitial has been shown. Pause / save state accordingly. } @Override public void onInterstitialClicked(MoPubInterstitial interstitial) {} @Override public void onInterstitialDismissed(MoPubInterstitial interstitial) { // 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:
@Override
protected void onPause() {
super.onPause();
MoPub.onPause(this);
}
@Override
protected void onStop() {
super.onStop();
MoPub.onStop(this);
}
@Override
protected void onResume() {
super.onResume();
MoPub.onResume(this);
}
// Additional lifecycle events
Last updated December 06, 2018
TWITTER, MOPUB, and the Bird logo are trademarks of Twitter, Inc. or its affiliates. All third party logos and trademarks included are the property of their respective owners.
© 2018 MoPub Inc.