Where should i call MobileAds.initialize()?

后端 未结 3 1780
失恋的感觉
失恋的感觉 2021-02-20 02:49

I have read https://developers.google.com/admob/android/quick-start?hl=en-US#import_the_mobile_ads_sdk

I need to initialize MobileAds using Code A in order to display Ad

3条回答
  •  面向向阳花
    2021-02-20 03:11

    From the docs of MobileAds.initialize():

    This method should be called as early as possible, and only once per application launch.

    The proper way to do this would be to call it in the onCreate() method of the Application class.

    If you don't have an Application class, simply create one, something like this:

    class YourApp: Application() {
    
        override fun onCreate() {
            super.onCreate()
            MobileAds.initialize(this, "YOUR_ADMOB_APP_ID")
        }
    }
    

    You have to reference this class in AndroidManifest.xml, by setting the android:name attribute of the application tag:

    
    
        
    
    
    

    And regarding your question:

    why can the AdMob Ad be displayed correctly even if I remove

    MobileAds.initialize(this, "YOUR_ADMOB_APP_ID")
    

    Quote from Veer Arjun Busani of the Mobile Ads SDK team:

    The Mobile Ads SDK take a few milliseconds to initialize itself and we now have provided this method to call it way before you even call your first ad. Once that is done, there would not be any added load time for your first request. If you do not call this, then your very first AdRequest would take a few milliseconds more as it first needs to initialize itself.

    So basically if you do not call MobileAds.initialize(), then the first AdRequest will call it implicitly.

提交回复
热议问题