How can i to show the Ad when i push the button for next activity?

Viewed 29
class MainActivity : AppCompatActivity() {
lateinit var mAdView : AdView
private var mInterstitialAd: InterstitialAd? = null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    loadBannerAd()
    loadInterAd()
    val wLista = findViewById<Button>(R.id.Lista)
    wLista.setOnClickListener{
        showInterAd()
        startActivity(Intent(this, com.tango.changapp.wLista::class.java))
    }
private fun showInterAd() {
    if(mInterstitialAd != null){
        mInterstitialAd?.fullScreenContentCallback = object : FullScreenContentCallback(){
            override fun onAdFailedToShowFullScreenContent(p0: AdError) {
                super.onAdFailedToShowFullScreenContent(p0)
            }
            override fun onAdShowedFullScreenContent() {
                super.onAdShowedFullScreenContent()
            }
            override fun onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent()
                val intent = Intent(this@MainActivity, MainActivity::class.java)
                startActivity(intent)
            }
            override fun onAdImpression() {
                super.onAdImpression()
            }
        }
        mInterstitialAd?.show(this)
    }else{
        val intent = Intent(this, MainActivity2::class.java)
        //startActivity(intent)
}
}
private fun loadInterAd() {
    var adRequest = AdRequest.Builder().build()
    InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest, object : InterstitialAdLoadCallback() {
        override fun onAdFailedToLoad(adError: LoadAdError) {
            mInterstitialAd = null
        }
        override fun onAdLoaded(interstitialAd: InterstitialAd) {
            mInterstitialAd = interstitialAd
        }
    })
}

I have the following code, and I want for an Ad to be launched when I advance to the next activity, but it doesn't happen, the Ad is only launched when I am in the other activity and I press the return button

0 Answers
Related