Load a single app open ad

App open ads are an ad format intended for publishers who want to monetize their app load screens. Your users can close app open ads at any time. When users bring your app to the foreground, show app open ads.

For more information, see App open ad guidance.

This guide explains how to integrate app open ads into an Android app.

Before you begin

Before you continue, do the following:

  • Set up GMA Next-Gen SDK.
  • Use the test app open ad unit ID ca-app-pub-3940256099942544/9257395921.
    • When building and testing your app, make sure that you use test ads rather than live, production ads. If you don't use the test ad unit ID, Google can suspend your account.
    • Before you publish your app, replace this ID with your ad unit ID.
    • For details on GMA Next-Gen SDK test ads, see Enable test ads.

Load an ad

The following example loads a single ad:

Kotlin

// Load ads after you initialize MobileAds.
AppOpenAd.load(
  AdRequest.Builder(adUnitId).build(),
  object : AdLoadCallback<AppOpenAd> {
    override fun onAdLoaded(ad: AppOpenAd) {
      // App open ad loaded.
      appOpenAd = ad
    }

    override fun onAdFailedToLoad(adError: LoadAdError) {
      // App open ad failed to load.
      Log.e(TAG, "App open ad failed to load: ${adError.message}")
      appOpenAd = null
    }
  },
)

Java

// Load ads after you initialize MobileAds.
AppOpenAd.load(
    new AdRequest.Builder(adUnitId).build(),
    new AdLoadCallback<AppOpenAd>() {
      @Override
      public void onAdLoaded(AppOpenAd ad) {
        // App open ad loaded.
        appOpenAd = ad;
      }

      @Override
      public void onAdFailedToLoad(LoadAdError adError) {
        // App open ad failed to load.
        Log.e(TAG, "App open ad failed to load: " + adError.getMessage());
        appOpenAd = null;
      }
    });

When testing, use the test ad unit ID: ca-app-pub-3940256099942544/9257395921.

Show the ad

To show an app open ad, call the show method. App open ads expire four hours after loading. Don't show an app open ad that is loading for more than four hours.

The following example displays the ad:

Kotlin

private fun showAd(appOpenAd: AppOpenAd, activity: Activity) {
  // Show the ad.
  appOpenAd.show(activity)
}

Java

private void showAd(AppOpenAd appOpenAd, Activity activity) {
  // Show the ad.
  appOpenAd.show(activity);
}

Handle cold starts and loading screens

A cold start occurs when your app launches from disk, and a warm start occurs when you reopen the app from memory. During a cold start, you don't have a previously loaded ad available to show. During a warm start, you might still have a loaded ad available to show.

During a cold start, users might interact with your app before it loads and shows an ad. To avoid a poor user experience, show app open ads on cold starts strictly from a loading screen while app assets load. While your users wait for your app to load, show app open ads. Don't show an app open ad on the first app start. If asset loading completes and users reach the main content before the ad loads, don't show the ad.

Load app assets on a background thread for loading to continue while the ad shows. If your loading screen finishes loading while the ad shows, dismiss the loading screen in the ad dismissed callback method.

Listen to ad events

Before showing the ad, listen to ad lifecycle events. The following example registers callbacks for ad events:

Kotlin

private fun listenToAdEvents() {
  // Listen for ad events.
  val ad = appOpenAd
  if (ad == null) {
    Log.e(TAG, "App open ad is not ready yet.")
    return
  }

  ad.adEventCallback =
    object : AppOpenAdEventCallback {
      override fun onAdShowedFullScreenContent() {
        // App open ad did show.
      }

      override fun onAdDismissedFullScreenContent() {
        // App open ad did dismiss.
        appOpenAd = null
      }

      override fun onAdFailedToShowFullScreenContent(
        fullScreenContentError: FullScreenContentError
      ) {
        // App open ad failed to show.
        Log.e(TAG, "App open ad failed to show: ${fullScreenContentError.message}")
      }

      override fun onAdImpression() {
        // App open ad did record an impression.
      }

      override fun onAdClicked() {
        // App open ad did record a click.
      }
    }
}

Java

private void listenToAdEvents() {
  // Listen for ad events.
  if (appOpenAd == null) {
    Log.e(TAG, "App open ad is not ready yet.");
    return;
  }

  appOpenAd.setAdEventCallback(
      new AppOpenAdEventCallback() {
        @Override
        public void onAdShowedFullScreenContent() {
          // App open ad did show.
        }

        @Override
        public void onAdDismissedFullScreenContent() {
          // App open ad did dismiss.
          appOpenAd = null;
        }

        @Override
        public void onAdFailedToShowFullScreenContent(
            @NonNull FullScreenContentError fullScreenContentError) {
          // App open ad failed to show.
          Log.e(TAG, "App open ad failed to show: " + fullScreenContentError.getMessage());
        }

        @Override
        public void onAdImpression() {
          // App open ad did record an impression.
        }

        @Override
        public void onAdClicked() {
          // App open ad did record a click.
        }
      });
}