Skip to content

Commit

Permalink
Remove unnecessary OnGlobalLayoutListener check in for Kotlin & Java …
Browse files Browse the repository at this point in the history
…banner samples

PiperOrigin-RevId: 666336199
  • Loading branch information
Justin Malandruccolo authored and copybara-github committed Aug 27, 2024
1 parent e04300a commit b32e79d
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public class MyActivity extends AppCompatActivity {
static final String AD_UNIT = "/21775744923/example/adaptive-banner";
private static final String TAG = "MyActivity";
private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false);
private final AtomicBoolean initialLayoutComplete = new AtomicBoolean(false);
private GoogleMobileAdsConsentManager googleMobileAdsConsentManager;
private AdManagerAdView adView;
private FrameLayout adContainerView;
Expand Down Expand Up @@ -91,18 +90,6 @@ protected void onCreate(Bundle savedInstanceState) {
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.
adContainerView
.getViewTreeObserver()
.addOnGlobalLayoutListener(
() -> {
if (!initialLayoutComplete.getAndSet(true)
&& googleMobileAdsConsentManager.canRequestAds()) {
loadBanner();
}
});
}

@Override
Expand Down Expand Up @@ -212,12 +199,7 @@ private void initializeMobileAdsSdk() {
MobileAds.initialize(this, initializationStatus -> {});

// Load an ad on the main thread.
runOnUiThread(
() -> {
if (initialLayoutComplete.get()) {
loadBanner();
}
});
runOnUiThread(this::loadBanner);
})
.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public class MyActivity extends AppCompatActivity {
private GoogleMobileAdsConsentManager googleMobileAdsConsentManager;
private AdView adView;
private FrameLayout adContainerView;
private final AtomicBoolean initialLayoutComplete = new AtomicBoolean(false);

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -91,18 +90,6 @@ protected void onCreate(Bundle savedInstanceState) {
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.
adContainerView
.getViewTreeObserver()
.addOnGlobalLayoutListener(
() -> {
if (!initialLayoutComplete.getAndSet(true)
&& googleMobileAdsConsentManager.canRequestAds()) {
loadBanner();
}
});
}

@Override
Expand Down Expand Up @@ -210,12 +197,7 @@ private void initializeMobileAdsSdk() {
MobileAds.initialize(this, initializationStatus -> {});

// Load an ad on the main thread.
runOnUiThread(
() -> {
if (initialLayoutComplete.get()) {
loadBanner();
}
});
runOnUiThread(this::loadBanner);
})
.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import kotlinx.coroutines.launch
class MyActivity : AppCompatActivity() {

private val isMobileAdsInitializeCalled = AtomicBoolean(false)
private val initialLayoutComplete = AtomicBoolean(false)
private var adView: AdManagerAdView? = null
private lateinit var binding: ActivityMyBinding
private lateinit var googleMobileAdsConsentManager: GoogleMobileAdsConsentManager
Expand Down Expand Up @@ -91,14 +90,6 @@ class MyActivity : 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 MyActivity : AppCompatActivity() {

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

0 comments on commit b32e79d

Please sign in to comment.