Marcel Skotnicki
Marcel Skotnicki

Reputation: 126

AdMob RewardedVideoAd keeps running in background

So I have a field:

private lateinit var adMobAd: RewardedVideoAd

and I initialize it in onCreate() method:

adMobAd = MobileAds.getRewardedVideoAdInstance(this)

which I destroy in onDestroy() method:

public override fun onDestroy() { adMobAd.destroy(this) super.onDestroy() }

but when I click the button to load an ad and then leave activity quickly, an ad is showed few seconds later (i show ad in onRewardedVideoAdLoaded() callback). It is very bad behaviour, specially when ad is not skippable. It also calls callback when it is done, so I got crashes of course. In addition, I use some mediations to my rewarded videos like

implementation 'com.google.ads.mediation:adcolony:3.1.2.0' implementation 'com.google.ads.mediation:tapjoy:11.11.0.0' implementation 'com.google.ads.mediation:unity:2.1.0.0'

How can I prevent this case?

Upvotes: 0

Views: 904

Answers (2)

Appafly
Appafly

Reputation: 666

OnDestroy isnt always called, so also use OnPause, so one of them will be called. In your Activity, destroy the ad in the onPause method. Add this code:

@Override
protected void onPause() {
    super.onPause();
    adMobAd.destroy(this);
}

Hope this helps!

Upvotes: 0

Angel Koh
Angel Koh

Reputation: 13515

you can try to add a flag and check if the ads is available first before you show the ad?

fun onDestroy(){ isDestroyed =true  ...}

in the button click, you check if the ad is already loaded or the activity is being queued to be destroyed.

if(admobAd.isLoaded() && !isDestroyed){ admobAd.show() }

Upvotes: 1

Related Questions