How to set Interstitial's Ad listener in SDK 20.0.0?

Viewed 1000

I tried to set the adListener for Interstitial Ad but it doesn't work (AdMob + Kotlin - Mobile Ads Garage #13). After a bit digging I found that the methods adListener is deprecated, and is replaced with Instertitial Ad.load. I really don't know how to set a listener on ad this way(??) and I have searched all over the internet to find a solution for this. Please help. Thank you.


Edit: I want to navigate to new fragment when the Instertitial Ad is dismissed (closed), so I tried:

private fun showInterstitial() {
        mInterstitialAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
            override fun onAdDismissedFullScreenContent() {
                Log.d(TAG, "Ad was dismissed.")
                // Don't forget to set the ad reference to null so you
                // don't show the ad a second time.
                mInterstitialAd = null
                view?.findNavController()?.navigate(action)
            }

            override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
                Log.d(TAG, "Ad failed to show.")
                // Don't forget to set the ad reference to null so you
                // don't show the ad a second time.
                mInterstitialAd = null
                view?.findNavController()?.navigate(action)

            }

            override fun onAdShowedFullScreenContent() {
                Log.d(TAG, "Ad showed fullscreen content.")
                view?.findNavController()?.navigate(action)
            }
        }
        mInterstitialAd?.show(this.activity)}

and It doesn't work.

4 Answers

In the latest implementation, you can listen to Interstitial ads via two callbacks. The first callback is for Ad loading, here you can check whether the Ad is successfully loaded or not. Once loaded, you can assign it to your local InterstitialAd variable, for example, mInterstitialAd and then you can show the Ad to the user.

InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest, object : InterstitialAdLoadCallback() {
        override fun onAdFailedToLoad(adError: LoadAdError) {
          Log.d(TAG, adError?.message)
          mInterstitialAd = null
        }

        override fun onAdLoaded(interstitialAd: InterstitialAd) {
          Log.d(TAG, 'Ad was loaded.')
          mInterstitialAd = interstitialAd
        }
      })

The second callback can be used for recieving callbacks once the Ad is shown to user:

mInterstitialAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
  override fun onAdDismissedFullScreenContent() {
    Log.d(TAG, 'Ad was dismissed.')
  }

  override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
    Log.d(TAG, 'Ad failed to show.')
  }

  override fun onAdShowedFullScreenContent() {
    Log.d(TAG, 'Ad showed fullscreen content.')
    mInterstitialAd = null;
  }
}

Now you can show the Ad:

if (mInterstitialAd != null) {
  mInterstitialAd?.show(this)
} else {
  Log.d("TAG", "The interstitial ad wasn't ready yet.")
}

Refer to https://developers.google.com/admob/android/interstitial?hl=en-US#load_an_ad for more details.

You should insert this

Intent intent = new Intent(MainActivityGame.this, MainActivity.class);
startActivity(intent);
finish();

in method onAdDismissedFullScreenContent()

public void loadAd() {
            AdRequest adRequest = new AdRequest.Builder().build();
            InterstitialAd.load(
                    this,
                    "ca-app-pub-3940256099942544/1033173712",
                    adRequest,
                    new InterstitialAdLoadCallback() {
                        @Override
                        public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                            // The mInterstitialAd reference will be null until
                            // an ad is loaded.
                            MainActivityGame.this.interstitialAd = interstitialAd;
                            Log.i(TAG, "onAdLoaded");
                            Toast.makeText(MainActivityGame.this, "onAdLoaded()", Toast.LENGTH_SHORT).show();
                            interstitialAd.setFullScreenContentCallback(
                                    new FullScreenContentCallback() {
                                        @Override
                                        public void onAdDismissedFullScreenContent() {
                                            // Called when fullscreen content is dismissed.
                                            // Make sure to set your reference to null so you don't
                                            // show it a second time.
                                            MainActivityGame.this.interstitialAd = null;
                                            Log.d("TAG", "The ad was dismissed.");
                                            Intent intent = new Intent(MainActivityGame.this, MainActivity.class);
                                            startActivity(intent);
                                            finish();
                                        }
    
                                        @Override
                                        public void onAdFailedToShowFullScreenContent(AdError adError) {
                                            // Called when fullscreen content failed to show.
                                            // Make sure to set your reference to null so you don't
                                            // show it a second time.
                                            MainActivityGame.this.interstitialAd = null;
                                            Log.d("TAG", "The ad failed to show.");
                                        }
    
                                        @Override
                                        public void onAdShowedFullScreenContent() {
                                            // Called when fullscreen content is shown.
                                            Log.d("TAG", "The ad was shown.");
                                            //Intent intent = new Intent(MainActivityGame.this, MainActivity.class);
                                            //startActivity(intent);
                                            //finish();
    
                                        }
    
                                        public void onAdClosed() {
                                            Intent intent = new Intent(MainActivityGame.this, MainActivity.class);
                                            startActivity(intent);
                                            finish();
    
                                        }
                                    });
                        }
    
                        @Override
                        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                            // Handle the error
                            Log.i(TAG, loadAdError.getMessage());
                            interstitialAd = null;
    
                            String error =
                                    String.format(
                                            "domain: %s, code: %d, message: %s",
                                            loadAdError.getDomain(), loadAdError.getCode(), loadAdError.getMessage());
                            Toast.makeText(
                                    MainActivityGame.this, "onAdFailedToLoad() with error: " + error, Toast.LENGTH_SHORT)
                                    .show();
                        }
                    });
        }

It is late but I was also figuring out how to navigate to other fragment when a user dismisses the Interstitial Ad. so here's the solution-

Use navigation-safeargs in onAdDismissedFullScreenContent() callback instead of navigation component (coz there you will not required view) to navigate in fragments.

    override fun onAdDismissedFullScreenContent() {
               
         val action = VideoCallFragmentDirections.action_f1_to_f2()
                      findNavController().navigate(action)

              }
onClick = {
showInterstitial()
mInterstitialAd.fullScreenContentCallback = object: FullScreenContentCallback(){
  
override fun onAdDismissedFullScreenContent(){

navController.navigate("fragment route")
}

}
Related