RewardedVideoAd callback listener mismatch
I have made one common module for showing RewardedVideoAd
.you can try this. I think it may solve your problem.
put below the class in your project.
AdRewardedHandler.java
public class AdRewardedHandler {
private static final String TAG = "AdRewardedHandler";
private Context context;
private static AdRewardedHandler adRewardedHandler;
private RewardedVideoAd rewardedVideoAd;
private RewardedAdListener rewardedAdListener;
private boolean isFailedToLoad = false;
public static AdRewardedHandler getInstance() {
if (adRewardedHandler == null) {
adRewardedHandler = new AdRewardedHandler();
}
return adRewardedHandler;
}
public void initializeRewardedHandler(Context context) {
this.context = context;
rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(context);
loadRewardedVideoAd();
}
public void setAdHandlerListener(RewardedAdListener rewardedAdListener1) {
this.rewardedAdListener = rewardedAdListener1;
}
public boolean isAdLoaded() {
if (rewardedVideoAd != null) {
return rewardedVideoAd.isLoaded();
} else {
return false;
}
}
public void loadRewardedVideoAd() {
rewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",new AdRequest.Builder().build());
rewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
@Override
public void onRewardedVideoAdLoaded() {
Log.i(TAG, "onRewardedVideoAdLoaded: ");
if (rewardedAdListener != null) {
rewardedAdListener.onRewardedVideoAdLoaded();
}
}
@Override
public void onRewardedVideoAdOpened() {
Log.i(TAG, "onRewardedVideoAdOpened: ");
if (rewardedAdListener != null) {
rewardedAdListener.onRewardedVideoAdOpened();
}
}
@Override
public void onRewardedVideoStarted() {
Log.i(TAG, "onRewardedVideoStarted: ");
if (rewardedAdListener != null) {
rewardedAdListener.onRewardedVideoStarted();
}
}
@Override
public void onRewardedVideoAdClosed() {
Log.i(TAG, "onRewardedVideoAdClosed: ");
if (rewardedAdListener != null) {
rewardedAdListener.onRewardedVideoAdClosed();
if (!isAdLoaded()) {
loadRewardedVideoAd();
}
}
}
@Override
public void onRewarded(RewardItem var1) {
Log.i(TAG, "onRewarded: " + var1);
if (rewardedAdListener != null) {
rewardedAdListener.onRewarded(var1);
}
}
@Override
public void onRewardedVideoAdLeftApplication() {
Log.i(TAG, "onRewardedVideoAdLeftApplication: ");
if (rewardedAdListener != null) {
rewardedAdListener.onRewardedVideoAdLeftApplication();
}
}
@Override
public void onRewardedVideoAdFailedToLoad(int var1) {
Log.i(TAG, "onRewardedVideoAdFailedToLoad: " + var1);
if (!isFailedToLoad) {
isFailedToLoad = true;
if (!isAdLoaded()) {
loadRewardedVideoAd();
}
}
if (rewardedAdListener != null) {
rewardedAdListener.onRewardedVideoAdFailedToLoad(var1);
}
}
@Override
public void onRewardedVideoCompleted() {
Log.i(TAG, "onRewardedVideoCompleted: ");
if (rewardedAdListener != null) {
rewardedAdListener.onRewardedVideoCompleted();
if (!isAdLoaded()) {
loadRewardedVideoAd();
}
}
}
});
}
public void showRewardedAd(RewardedAdListener rewardedAdListener1) {
setAdHandlerListener(rewardedAdListener1);
Log.i(TAG, "showRewardedAd: ");
try {
if (rewardedVideoAd != null && rewardedVideoAd.isLoaded()) {
rewardedVideoAd.show();
}
} catch (Throwable th) {
th.printStackTrace();
}
}
public void onResume() {
Log.i(TAG, "onResume: Resume Call :");
if (rewardedVideoAd != null) {
rewardedVideoAd.resume(context);
}
}
public void onPause() {
Log.i(TAG, "onPause: Pause Call : ");
if (rewardedVideoAd != null) {
rewardedVideoAd.pause(context);
}
}
public void onDestroy() {
Log.i(TAG, "onDestroy: Destroy Call :");
}
public interface RewardedAdListener {
void onRewardedVideoAdLoaded();
void onRewardedVideoAdOpened();
void onRewardedVideoStarted();
void onRewardedVideoAdClosed();
void onRewarded(RewardItem var1);
void onRewardedVideoAdLeftApplication();
void onRewardedVideoAdFailedToLoad(int var1);
void onRewardedVideoCompleted();
}
}
Put below line in your application file.
AdRewardedHandler.getInstance().initializeRewardedHandler(this);
Now, finally How to use
public class YourActivity extends AppCompatActivity implements AdRewardedHandler.RewardedAdListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AdRewardedHandler.getInstance().setAdHandlerListener(this);
if (!AdRewardedHandler.getInstance().isAdLoaded()) {
AdRewardedHandler.getInstance().loadRewardedVideoAd();
}
}
public void showRewardedVideoAd() {
// showRewardedAd
AdRewardedHandler.getInstance().showRewardedAd(this);
}
@Override
public void onResume() {
AdRewardedHandler.getInstance().onResume();
super.onResume();
}
@Override
public void onPause() {
AdRewardedHandler.getInstance().onPause();
super.onPause();
}
@Override
public void onRewardedVideoAdLoaded() {
Log.i(TAG, "onRewardedVideoAdLoaded: ");
}
@Override
public void onRewardedVideoAdOpened() {
Log.i(TAG, "onRewardedVideoAdOpened: ");
}
@Override
public void onRewardedVideoStarted() {
Log.i(TAG, "onRewardedVideoStarted: ");
}
@Override
public void onRewardedVideoAdClosed() {
Log.i(TAG, "onRewardedVideoAdClosed: ");
}
@Override
public void onRewardedVideoAdLeftApplication() {
Log.i(TAG, "onRewardedVideoAdLeftApplication: ");
}
@Override
public void onRewardedVideoAdFailedToLoad(int var1) {
Log.i(TAG, "onRewardedVideoAdFailedToLoad: " + var1);
}
@Override
public void onRewardedVideoCompleted() {
Log.i(TAG, "onRewardedVideoCompleted: ");
}
}
I was facing the same problem. In my case, When I am going to Activity A to B so, my Activity A in background and Activity B is on foreground, now when i'm back to Activity A that time I don't get callback of RewardedVideoAds.
I was solved this problem in simple way, when i'm back to Activity A that time I set the listener in OnResume Method.
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume Call.");
if (rewardedVideoAd != null) {
rewardedVideoAd.setRewardedVideoAdListener(this);
rewardedVideoAd.resume(activity);
}
}
Note that for this solution you have to remove this two line from your Activity B.
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy: ");
rewardedVideoAd.destroy(activity);
rewardedVideoAd.setRewardedVideoAdListener(null);
}