Skip to content

Commit

Permalink
Refactored loadBanner()
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 666333573
  • Loading branch information
Justin Malandruccolo authored and copybara-github committed Aug 22, 2024
1 parent 9d12721 commit d03a06b
Showing 1 changed file with 1 addition and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {

private val isMobileAdsInitializeCalled = AtomicBoolean(false)
private val initialLayoutComplete = AtomicBoolean(false)
private var adView: AdView? = null
private lateinit var binding: ActivityMainBinding
private lateinit var googleMobileAdsConsentManager: GoogleMobileAdsConsentManager
Expand Down Expand Up @@ -91,14 +90,6 @@ class MainActivity : AppCompatActivity() {
if (googleMobileAdsConsentManager.canRequestAds) {
initializeMobileAdsSdk()
}

// Since we're loading the banner based on the adContainerView size, we need to wait until this
// view is laid out before we can get the width.
binding.adViewContainer.viewTreeObserver.addOnGlobalLayoutListener {
if (!initialLayoutComplete.getAndSet(true) && googleMobileAdsConsentManager.canRequestAds) {
loadBanner()
}
}
}

/** Called when leaving the activity. */
Expand Down Expand Up @@ -185,9 +176,7 @@ class MainActivity : AppCompatActivity() {

runOnUiThread {
// Load an ad on the main thread.
if (initialLayoutComplete.get()) {
loadBanner()
}
loadBanner()
}
}
}
Expand Down

0 comments on commit d03a06b

Please sign in to comment.