Update (March 24, 2015): Per recent policy updates, interstitial ads on app-load are not recommended. See the AdMob help center for the latest interstitial policy guidance.
Previously, we showed you how to create a splash screen interstitial on iOS . Today we’ll discuss how to trigger an interstitial on an Android app launch.
The cleanest solution is to have an app launch Activity
for showing the splash screen image and loading/showing the interstitial. This special splash screen activity should be the activity that launches when the user starts the app. In the splash screen activity’s onCreate
method, the first task is to make an ImageView
and set it as the content view.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView image = new ImageView(this);
// Assumes you have a resource with the name kitten.
image.setImageResource(R.drawable.kitten);
setContentView(image);
Next, create and load the interstitial ad. Setting an AdListener
is needed in order to know when the ad succeeds or fails to load. If the interstitial loads within a reasonable time limit, we’ll show it. If it fails, we’ll move on to the main activity.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(AD_UNIT_ID);
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
if (!interstitialCanceled) {
waitTimer.cancel();
interstitial.show();
}
}
@Override
public void onAdFailedToLoad(int errorCode) {
startMainActivity();
}
});
The “reasonable time limit” mentioned earlier is enforced by creating a timer to stop waiting for the interstitial. If the interstitial doesn’t load fast enough (in this case 5 seconds), we skip it and proceed to the main activity.
waitTimer = new Timer();
waitTimer.schedule(new TimerTask() {
@Override
public void run() {
interstitialCanceled = true;
SplashScreenActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
startMainActivity();
}
});
}
}, 5000);
} // end of onCreate implementation.
private void startMainActivity() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
Handing Early Exits
The code so far assumes that the user will stay in the activity for the duration of the interstitial loading. But what if the user hits the back or home button before the interstitial loads or the timer goes off? The interstitial will actually continue loading and could be shown on top of the home screen! That would be a poor user experience.
We account for this edge case by implementing the onPause
and onResume
methods from the activity lifecycle . In onPause
, we stop the timer and set the interstitialCanceled
flag so the interstitial doesn’t get immediately shown. In onResume
, we show the interstitial if it’s ready when the user returns to the app again, otherwise we start the main activity.
@Override
public void onPause() {
waitTimer.cancel();
interstitialCanceled = true;
super.onPause();
}
@Override
public void onResume() {
super.onResume();
if (interstitial.isLoaded()) {
interstitial.show();
} else if (interstitialCanceled) {
startMainActivity();
}
}
If you’re wondering why interstitialCanceled
is checked again before starting the main application, it’s because onResume
gets called immediately after onCreate the first time the app is loaded. And on the first app launch, we do want to wait for the interstitial to load.
A complete implementation is available in our googleads-mobile-android-examples repo on GitHub. Give us a shout on the forum if you have any questions about implementing the Google Mobile Ads SDK in your mobile applications. You can also find us on Google+ .
- Eric Leichtenschlag , Mobile Ads Developer Relations