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 on app startup.
KotlinJavaYandexAds.initialize(this) { // Now you can use ads }YandexAds.initialize(this, () -> { // Now you can use ads }); -
Create and set up the
AppOpenAdLoaderad loader object.You'll need the ad placement ID obtained in the Yandex Advertising Network interface (
AD_UNIT_ID).You can extend ad request parameters with the
AdRequest.Builder()class to include information about the user's interests, page context, location, and other additional data in the ad request. Adding extra context to ad requests can greatly improve ad relevance.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 = 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 the
AppOpenAdLoadListenercallback method listener for notifications when ads load successfully or unsuccessfully.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 the
loadAd(AdRequest, AppOpenAdLoadListener)method.KotlinJavaprivate fun loadAppOpenAd() { appOpenAdLoader.loadAd(adRequest, appOpenAdLoadListener) }private void loadAppOpenAd() { appOpenAdLoader.loadAd(adRequest, appOpenAdLoadListener); } -
Use
LifecycleEventObserverto handle app status changes and display app open ads.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 rendering the ad, set the
AppOpenAdEventListenerad callback method listener.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); } -
Serve the ad using the
showmethod.KotlinJavaprivate fun showAppOpenAd() { appOpenAd?.show(activity) }private void showAppOpenAd() { if (mAppOpenAd != null) { mAppOpenAd.show(activity); } } -
Release the resources.
That prevents 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 SDK integration is successful by searching the YandexAds keyword in Logcat:
[Integration] Ad type App Open was integrated successfully