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 AdMob AD.
I have some activities which need to display ADs, do I need to add code A in my all activities?
And more, why can the AdMob Ad be displayed correctly even if I remove
MobileAds.initialize(this, "YOUR_ADMOB_APP_ID")
Code A
import com.google.android.gms.ads.MobileAds;
class MainActivity : AppCompatActivity() {
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
MobileAds.initialize(this, "YOUR_ADMOB_APP_ID")
}
...
}
From the docs of
MobileAds.initialize()
:The proper way to do this would be to call it in the
onCreate()
method of theApplication
class.If you don't have an
Application
class, simply create one, something like this:You have to reference this class in AndroidManifest.xml, by setting the
android:name
attribute of theapplication
tag:And regarding your question:
Quote from Veer Arjun Busani of the Mobile Ads SDK team:
So basically if you do not call
MobileAds.initialize()
, then the firstAdRequest
will call it implicitly.