androidkotlinadmobinterstitial

How to reload Interstitial Google Ads (New Version) in Kotlin Android


I have added Interstitial Google Ads in my Kotlin project. It is loading only once and when I am trying to re-show it, it's not showing.

// Defined variable on top
private var mInterstitialAd: InterstitialAd? = null

    MobileAds.initialize(requireContext()) {}
    val adRequest = AdRequest.Builder().build()
    adView.loadAd(adRequest)


        InterstitialAd.load(requireContext(),"---Interstitial Unit id---", adRequest, object : InterstitialAdLoadCallback() {
            override fun onAdFailedToLoad(adError: LoadAdError) {
                Log.d("LoadAdTAG", adError?.message)
                mInterstitialAd = null
            }

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

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

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

            override fun onAdShowedFullScreenContent() {
                Log.d("LoadAdTAG", "Ad showed fullscreen content.")
//                mInterstitialAd = null
            }
        }

And then I am showing it on some condition after some time. The first time it is showing as below

mInterstitialAd?.show(requireActivity())

But next time when trying to show, this line gets called but Ad is not showing.

Could you please help me out with how to show the ad again?

I have followed below Google Docs (new version): https://developers.google.com/admob/android/interstitial


Solution

  • add this code:-

    fun loadAds() {
        val adRequest = AdRequest.Builder().build()
        InterstitialAd.load(requireActivity(), interstitial_id, adRequest, object : InterstitialAdLoadCallback() {
            override fun onAdLoaded(interstitialAds: InterstitialAd) {
                mInterstitialAd = interstitialAds
    
                mInterstitialAd!!.fullScreenContentCallback = object : FullScreenContentCallback() {
                    override fun onAdDismissedFullScreenContent() {
                        loadAds()
                    }
    
                    override fun onAdFailedToShowFullScreenContent(adError: AdError) {}
                    override fun onAdShowedFullScreenContent() {
                        mInterstitialAd = null
                    }
                }
            }
    
            override fun onAdFailedToLoad(loadAdError: LoadAdError) {
                mInterstitialAd = null
            }
        })
    }
    

    add this code for show ads:-

    if (mInterstitialAd != null) {
            mInterstitialAd!!.show(requireActivity())
        }