App open ads
Note
Examples showing how all the format types work are available in the demo project.
Example of creating an app open ad
-
Initialize the SDK when the application starts.
KotlinJavaYandexAds.initialize(this) { // Now you can use ads }YandexAds.initialize(this, () -> { // Now you can use ads }); -
Create and configure the
AppOpenAdLoaderad loader.You need the ad unit ID from the Yandex Advertising Network interface (
AD_UNIT_ID).You can extend the ad request via
AdRequest.Builder()by passing user interests, page context, location, or other data. Extra contextual data in the request can significantly improve ad quality.KotlinJavaval appOpenAdLoader: AppOpenAdLoader = AppOpenAdLoader(application) val AD_UNIT_ID = "R-M-XXXXXX-Y" // for debugging you can use "demo-appopenad-yandex" val adRequest = AdRequest.Builder(AD_UNIT_ID).build()final AppOpenAdLoader appOpenAdLoader = new AppOpenAdLoader(application); final String AD_UNIT_ID = "R-M-XXXXXX-Y"; // for debugging you can use "demo-appopenad-yandex" final AdRequest adRequest = new AdRequest.Builder(AD_UNIT_ID).build(); -
Set an
AppOpenAdLoadListeneron the loader to receive success or failure load notifications.KotlinJavaval appOpenAdLoadListener = object : AppOpenAdLoadListener { override fun onAdLoaded(appOpenAd: AppOpenAd) { // The ad was loaded successfully. Now you can show loaded ad. this@Activity.appOpenAd = appOpenAd } override fun onAdFailedToLoad(adRequestError: AdRequestError) { // Ad failed to load with AdRequestError. // Attempting to load a new ad from the onAdFailedToLoad() method is strongly discouraged. } }AppOpenAdLoadListener appOpenAdLoadListener = new AppOpenAdLoadListener() { @Override public void onAdLoaded(@NonNull final AppOpenAd appOpenAd) { // The ad was loaded successfully. Now you can show loaded ad. mAppOpenAd = appOpenAd; } @Override public void onAdFailedToLoad(@NonNull final AdRequestError adRequestError) { // Ad failed to load with AdRequestError. // Attempting to load a new ad from the onAdFailedToLoad() method is strongly discouraged. } }; -
Load the ad using
loadAd(AdRequest, AppOpenAdLoadListener).KotlinJavaprivate fun loadAppOpenAd() { appOpenAdLoader.loadAd(adRequest, appOpenAdLoadListener) }private void loadAppOpenAd() { appOpenAdLoader.loadAd(adRequest, appOpenAdLoadListener); } -
Use
LifecycleEventObserverto handle application state changes and show the ad when the app is opened.KotlinJavaval processLifecycleObserver = DefaultProcessLifecycleObserver( onProcessCameForeground = ::showAppOpenAd ) ProcessLifecycleOwner.get().lifecycle.addObserver(processLifecycleObserver)final DefaultProcessLifecycleObserver processLifecycleObserver = new DefaultProcessLifecycleObserver() { @Override public void onProcessCameForeground() { showAppOpenAd(); } }; ProcessLifecycleOwner.get().getLifecycle().addObserver(processLifecycleObserver); -
Before showing the ad, set the ad event callback listener
AppOpenAdEventListener.KotlinJavaprivate inner class AdEventListener : AppOpenAdEventListener { override fun onAdShown() { // Called when ad is shown. } override fun onAdFailedToShow(adError: AdError) { // Called when ad failed to show. } override fun onAdDismissed() { // Called when ad is dismissed. // Clean resources after dismiss and preload new ad. clearAppOpenAd() loadAppOpenAd() } override fun onAdClicked() { // Called when a click is recorded for an ad. } override fun onAdImpression(impressionData: ImpressionData?) { // Called when an impression is recorded for an ad. // Get Impression Level Revenue Data in argument. } } private val appOpenAdEventListener = AdEventListener() appOpenAd?.setAdEventListener(appOpenAdEventListener)AppOpenAdEventListener appOpenAdEventListener = new AppOpenAdEventListener() { @Override public void onAdShown() { // Called when ad is shown. } @Override public void onAdFailedToShow(@NonNull final AdError adError) { // Called when ad failed to show. } @Override public void onAdDismissed() { // Called when ad is dismissed. // Clean resources after dismiss and preload new ad. clearAppOpenAd(); loadAppOpenAd(); } @Override public void onAdClicked() { // Called when a click is recorded for an ad. } @Override public void onAdImpression(@Nullable final ImpressionData impressionData) { // Called when an impression is recorded for an ad. } }; if (mAppOpenAd != null) { mAppOpenAd.setAdEventListener(appOpenAdEventListener); } -
Show the ad using the
showmethod.KotlinJavaprivate fun showAppOpenAd() { appOpenAd?.show(activity) }private void showAppOpenAd() { if (mAppOpenAd != null) { mAppOpenAd.show(activity); } } -
Release resources.
This is required to prevent memory leaks.
KotlinJavaprivate fun clearAppOpenAd() { appOpenAd?.setAdEventListener(null) appOpenAd = null }private void clearAppOpenAd() { if (mAppOpenAd != null) { mAppOpenAd.setAdEventListener(null); mAppOpenAd = null; } }
Checking integration
Build and run your project. You can check if the integration is successful by searching the YandexAds keyword in Logcat:
[Integration] Ad type App Open was integrated successfully