Rewarded ads

Rewarded ads are a full-screen ad format where users get bonuses or other rewards (for example, in-game coins, retries, access to premium features, and so on) for watching ads.

Note

The user decides whether to skip the ad or watch it and get a reward.

Note

Examples showing how all the format types work are available in the demo project.

Example of creating a rewarded ad

Key steps for integrating rewarded ads:

  • Create and configure a RewardedAdLoader.
  • Register the listener for ad load callback methods: RewardedAdLoadListener.
  • Load the ad.
  • Register a RewardedAdEventListener to listen for ad event callbacks.
  • Display the RewardedAd object.
  • Reward the user for viewing the ad.

Features of rewarded ad integration

  1. All calls to Yandex Mobile Ads SDK methods must be made from the main thread.

  2. If the onAdFailedToLoad() callback returns an error, don't try to load a new ad again. If there's no other option, limit the number of ad load retries. This will help avoid constant unsuccessful requests and connection issues if there are limitations.

  3. We recommend maintaining a strong reference to the loader and ad throughout the lifecycle of the screen that hosts interactions with the ad.

  4. We recommend using a single instance of RewardedAdLoader for all ad loads to improve performance.

Loading ads

To load rewarded ads, create and set up the RewardedAdLoader object.

For that, you'll need Activity or Application Context.

To notify when ads load or fail to load, set the RewardedAdLoadListener callback method listener for the RewardedAdLoader object.

To load an ad, you'll need the ad unit ID (adUnitId).

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.

Once the ad is loaded (by calling the onAdLoaded(rewardedAd: RewardedAd) method), save the link to the loaded RewardedAd before its display is completed.

The following example shows how to load a rewarded ad from Activity:

class RewardedAdActivity : AppCompatActivity(R.layout.activity_rewarded_ad) {
    private var rewardedAd: RewardedAd? = null
    private var rewardedAdLoader: RewardedAdLoader? = null
    private lateinit var binding: ActivityRewardedAdBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRewardedAdBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // Rewarded ads loading should occur after initialization of the SDK.
        // Initialize SDK as early as possible, for example in Application.onCreate or Activity.onCreate
        rewardedAdLoader = RewardedAdLoader(this)
        loadRewardedAd()
    }

    private fun loadRewardedAd() {
        val adRequest = AdRequest.Builder("your-ad-unit-id").build()
        rewardedAdLoader?.loadAd(adRequest, object : RewardedAdLoadListener {
            override fun onAdLoaded(ad: RewardedAd) {
                rewardedAd = ad
                // The ad was loaded successfully. Now you can show loaded ad.
            }

            override fun onAdFailedToLoad(adRequestError: AdRequestError) {
                // Ad failed to load with AdRequestError.
                // Attempting to load a new ad from the onAdFailedToLoad() method is strongly discouraged.
            }
        })
    }
}
public class RewardedAdActivity extends AppCompatActivity {
    @Nullable
    private RewardedAd mRewardedAd = null;
    @Nullable
    private RewardedAdLoader mRewardedAdLoader = null;
    private ActivityRewardedAdBinding mBinding;

    public RewardedAdActivity() {
        super(R.layout.activity_rewarded_ad);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityRewardedAdBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());

        // Ads loading should occur after initialization of the SDK.
        // Initialize SDK as early as possible, for example in Application.onCreate or Activity.onCreate
        mRewardedAdLoader = new RewardedAdLoader(this);
        loadRewardedAd();
    }

    private void loadRewardedAd() {
        if (mRewardedAdLoader != null) {
            final AdRequest adRequest =
                new AdRequest.Builder("your-ad-unit-id").build();
            mRewardedAdLoader.loadAd(adRequest, new RewardedAdLoadListener() {
                @Override
                public void onAdLoaded(@NonNull final RewardedAd rewardedAd) {
                    mRewardedAd = rewardedAd;
                    // The ad was loaded successfully. Now you can show loaded ad.
                }

                @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.
                }
            });
        }
    }
}

Displaying ads

To track the rewarded ad lifecycle and issue rewards, set the RewardedAdEventListener callback method listener for the RewardedAd object.

To render an ad, you'll need to pass the Activity to the show method of the loaded ad:

private fun showAd() {
   rewardedAd?.apply {
       setAdEventListener(object : RewardedAdEventListener {
           override fun onAdShown() {
               // Called when ad is shown.
           }

           override fun onAdFailedToShow(adError: AdError) {
           // Called when a RewardedAd failed to show

           // Clean resources after Ad failed to show
               rewardedAd?.setAdEventListener(null)
               rewardedAd = null

           // Now you can preload the next rewarded ad.
               loadRewardedAd()
           }

           override fun onAdDismissed() {
               // Called when ad is dismissed.
               // Clean resources after Ad dismissed
               rewardedAd?.setAdEventListener(null)
               rewardedAd = null

               // Now you can preload the next rewarded ad.
               loadRewardedAd()
           }

           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.
           }

           override fun onRewarded(reward: Reward) {
               // Called when the user can be rewarded.
           }
       })
       show(this@Activity)
   }
}
private void showAd() {
    if (mRewardedAd != null) {
        mRewardedAd.setAdEventListener(new RewardedAdEventListener() {
            @Override
            public void onAdShown() {
                // Called when ad is shown.
            }

            @Override
            public void onAdFailedToShow(@NonNull final AdError adError) {
                // Called when an ad failed to show.

                // Clean resources after Ad failed to show
              if (mRewardedAd != null) {
                  mRewardedAd.setAdEventListener(null);
                  mRewardedAd = null;
              }

              // Now you can preload the next ad.
              loadRewardedAd();
            }

            @Override
            public void onAdDismissed() {
                // Called when ad is dismissed.
                // Clean resources after Ad dismissed
                if (mRewardedAd != null) {
                    mRewardedAd.setAdEventListener(null);
                    mRewardedAd = null;
                }

                // Now you can preload the next ad.
                loadRewardedAd();
            }

            @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.
            }

            @Override
            public void onRewarded(@NonNull final Reward reward) {
                // Called when the user can be rewarded.
            }
        });
        mRewardedAd.show(this);
    }
}

Releasing resources

Don't store links to previously rendered ads. Call setAdEventListener(null) for previously rendered ads. That releases the resources and prevents memory leaks.

override fun onDestroy() {
    super.onDestroy()
    rewardedAdLoader = null
    destroyRewardedAd()
}

private fun destroyRewardedAd() {
    rewardedAd?.setAdEventListener(null)
    rewardedAd = null
}
@Override
protected void onDestroy() {
    super.onDestroy();
    mRewardedAdLoader = null;
    destroyRewardedAd();
}

private void destroyRewardedAd() {
    if (mRewardedAd != null) {
        mRewardedAd.setAdEventListener(null);
        mRewardedAd = 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 rewarded was integrated successfully