AI-generated Key Takeaways
-
Ad load and event callbacks from the Google Mobile Ads SDK run on a background thread.
-
UI-related operations within these callbacks must be explicitly dispatched to the UI thread.
-
Examples are provided in Kotlin and Java for adding a banner view to the view hierarchy after an ad loads.
This page covers the instructions to handle callbacks from a background thread.
Google Mobile Ads SDK (beta) runs ad load and event callbacks on a background thread. When performing UI-related operations within these callbacks, ensure you explicitly dispatch them to the UI thread.
The following examples add a banner view to view hierarchy after an ad loads:
Kotlin
BannerAd.load( adRequest, object : AdLoadCallback<BannerAd> { override fun onAdLoaded(ad: BannerAd) { // Add the banner view to the view hierarchy on the UI thread. activity?.runOnUiThread { binding.bannerViewContainer.addView(ad.getView(requireActivity())) } } }, )
Java
BannerAd.load( adRequest, new AdLoadCallback<BannerAd>() { @Override public void onAdLoaded(@NonNull BannerAd ad) { // Add the banner view to the view hierarchy on the UI thread. runOnUiThread( () -> binding.bannerViewContainer.addView(ad.getView(MainActivity.this))); } });