Skip to content

Commit e15cbc8

Browse files
Preload WebView provider on app startup
1 parent 37dee89 commit e15cbc8

File tree

1 file changed

+11
-0
lines changed
  • app/src/main/java/com/d4rk/androidtutorials/java/ads/managers

1 file changed

+11
-0
lines changed

app/src/main/java/com/d4rk/androidtutorials/java/ads/managers/AppOpenAd.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.app.Application.ActivityLifecycleCallbacks;
66
import android.content.Context;
77
import android.os.Bundle;
8+
import android.webkit.CookieManager;
89

910
import androidx.annotation.NonNull;
1011
import androidx.annotation.Nullable;
@@ -36,6 +37,16 @@ public void onCreate() {
3637
this.registerActivityLifecycleCallbacks(this);
3738
MobileAds.initialize(this, initializationStatus -> {
3839
});
40+
// Pre-warm the WebView system on the main thread. Some ad SDK calls such
41+
// as CookieManager.getInstance() may occur on background threads which
42+
// can cause deadlocks if the WebView provider has not been initialized
43+
// yet. Initializing it here prevents the "AdWorker" threads from
44+
// blocking on WebViewFactory.getProvider.
45+
try {
46+
CookieManager.getInstance();
47+
} catch (Exception ignored) {
48+
// Ignore WebView initialization errors; ads will simply not use cookies.
49+
}
3950
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
4051
appOpenAdManager = new AppOpenAdManager();
4152
}

0 commit comments

Comments
 (0)