Lazy AdMob ads loading

Viewed 1452

I'm loading a simple banner ad in my main activity. The problem is whenever I start application it stucks about 2 sec on white screen and then loads UI. The problem isn't about the app code, because before I added ads app used to launch instantly. This is the code I load ads:
in onCreate:

MobileAds.initialize(this, "ca-app-pub-#######")
        val mAdView: AdView = findViewById(R.id.adView)
        mAdView.visibility = View.GONE
        val adRequest = AdRequest.Builder().build()
        val adView = AdView(this)
        adView.adSize = AdSize.BANNER
        adView.adUnitId = "ca-app-pub-3940256099942544/6300978111"  <- test id
        initializeAds(mAdView, adRequest)

initializeAds():

 fun initializeAds(adView: AdView, adRequest: AdRequest){
        adView.loadAd(adRequest)
        adView.adListener = object : AdListener(){
            override fun onAdLoaded() {
                super.onAdLoaded()
                if (adView.visibility == View.GONE) {
                    adView.visibility = View.VISIBLE
                }
            }

            override fun onAdFailedToLoad(p0: Int) {
                super.onAdFailedToLoad(p0)
                adView.visibility = View.GONE
            }
        }
    }

Is there any way to load ad without UI lag?

4 Answers

Use this as banner ad id ca-app-pub-3940256099942544/6300978111

Domin is suggesting that you do a post delay of approx 2000ms for MobileAds.Initialize(). Please don't do that. After the 2secs the UI just freezes! The user will get super annoyed. It's better if the startup was slower than the app being non-responsive just when the user starts using it.

Okay guys, finally I found a quick workaround. I found MobileAds.initialize(context, id) and AdRequest.Builder().build() as making lag. Obviously activity won't be fully loaded unless onCreate is fully executed. To bypass that I used Handler to delay MobileAds.initialize and AdRequest.Builder().build and then it doesn't make UI lag. This is my updated code:

       // we can't delay UI tasks so we need initialize adView here - it's not heavy task anyway
        val mAdView: AdView = findViewById(R.id.adView)
        mAdView.visibility = View.GONE

        //delay ads to be displayed 2 sec after Activity is loaded
        Handler().postDelayed({
            MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713")
            initializeAds(mAdView)
        }, 2000)

initializeAds(AdView):

   fun initializeAds(adView: AdView) {
        adView.loadAd(AdRequest.Builder().build())
        adView.adListener = object : AdListener() {
            override fun onAdLoaded() {
                super.onAdLoaded()
                //if ad is loaded just show it
                if (adView.visibility == View.GONE) {
                    adView.visibility = View.VISIBLE
                }
            }

            override fun onAdFailedToLoad(p0: Int) {
                //if we can't show ad just hide it
                super.onAdFailedToLoad(p0)
                adView.visibility = View.GONE
            }
        }
    }

I hope it will help if anybody is struggling with alike problem

//Kotlin Code
//Heavy tasks in Second Thread
//finally loadAd() in UI thread
Thread{
MobileAds.initialize(this);
val mAdView = findViewById<AdView>(R.id.adview);
val adRequest = AdRequest.Builder().build();
runOnUiThread {
    mAdView.loadAd(adRequest);     
}}.start();
Related