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
Main steps for integrating rewarded ads:
- Create and configure the ad loader
RewardedAdLoader. - Register the load callback listener
RewardedAdLoadListener. - Load the ad.
- Register the ad event callback listener
RewardedAdEventListener. - Show the
RewardedAdad. - Grant the user a reward for watching the ad.
Rewarded ad integration specifics
-
All calls to Yandex Mobile Ads SDK methods must be made from the main thread.
-
If you receive an error in the
onAdFailedToLoad()callback, do not try to load a new ad again immediately. If you still need to retry, limit how many times you reload. That helps avoid endless failed requests and connectivity issues when the network is limited. -
Keep a strong reference to the loader and the ad for the entire lifetime of the screen where the user interacts with the ad.
-
Use a single
RewardedAdLoaderinstance for all rewarded ad loads to improve performance.
Loading ads
To load rewarded ads, create and configure a RewardedAdLoader object.
You need an Activity or Application Context.
To receive success or failure load notifications, set a RewardedAdLoadListener on the RewardedAdLoader.
You need the ad unit ID (adUnitId).
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.
After the ad loads (onAdLoaded(rewardedAd: RewardedAd)), keep a reference to the loaded RewardedAd until the ad has finished showing.
The example below shows how to load a rewarded ad from an 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 the ad
To track rewarded ad lifecycle events and receive the reward, set a RewardedAdEventListener on the RewardedAd object.
To show the ad, pass an Activity to the show method of the loaded ad object:
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@RewardedAdActivity)
}
}
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
Do not keep references to ads that have finished their lifecycle. Call setAdEventListener(null) for such ads.
This frees resources in use 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 integration is successful by searching the YandexAds keyword in Logcat:
[Integration] Ad type rewarded was integrated successfully