Broken colors in DayNight theme after loading Admob / Firebase ad
I am using a theme Theme.AppCompat.DayNight.NoActionBar
for my application. When I load adMob interstital some colors break in "night" mode (ie RecyclerView).
Screen:
These wrong colors are taken from the "night" values. When I close the application and start it again, everything is fine. When I kill the application I have the same situation.
Activity code:
public class MainActivity extends AppCompatActivity {
static {
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_AUTO);
}
private ArrayList<String> planetList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateRecycler();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
PlanetAdapter adapter = new PlanetAdapter(planetList, getApplicationContext());
recyclerView.setAdapter(adapter);
InterstitialAd interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId("ca-app-pub-543543543/543543543");
AdRequest adRequest = new AdRequest.Builder().build();
interstitialAd.loadAd(adRequest);
}
private void populateRecycler() {
for (int i = 0; i < 20; i++) {
planetList.add("TEST");
}
}
}
When I comment interstitialAd.loadAd(adRequest)
, everything is fine.
You can find the whole project here: GitHub
source to share
This issue is presumably caused by resetting UI mode WebView
and can be worked around by WebView
manually instantiating it .
After that, I didn't see the problem ( Application.oncreate()
in this particular application):
if (nightMode != AppCompatDelegate.MODE_NIGHT_NO) {
Log.d(TAG, "Manually instantiating WebView to avoid night mode issue.");
try {
new WebView(getApplicationContext());
} catch (Exception e) {
Log.e(TAG, "Got exception while trying to instantiate WebView to avoid night mode issue. Ignoring problem.", e);
}
}
AppCompatDelegate.setDefaultNightMode(nightMode);
Source: https://groups.google.com/forum/#!msg/google-admob-ads-sdk/OZzHq_-wAFY/K50jClZcBAAJ
source to share
We have used this approach and recently we noticed that we are having crashes on Samsung devices with Android P. Here is the crash report we receive
We have an new WebView(getApplicationContext());
application in our code.
java.lang.RuntimeException:
at android.app.ActivityThread.handleBindApplication (ActivityThread.java:6163)
at android.app.ActivityThread.access$1200 (ActivityThread.java:235)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1768)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loop (Looper.java:214)
at android.app.ActivityThread.main (ActivityThread.java:6986)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1445)
Caused by: java.lang.RuntimeException:
at org.chromium.android_webview.AwBrowserProcess.a (PG:40)
at vo.c (PG:88)
at vo.b (PG:152)
at vo.a (PG:133)
at com.android.webview.chromium.WebViewChromiumFactoryProvider.a (PG:239)
at com.android.webview.chromium.WebViewChromium.init (PG:44)
at android.webkit.WebView.<init> (WebView.java:678)
at android.webkit.WebView.<init> (WebView.java:604)
at android.webkit.WebView.<init> (WebView.java:587)
at android.webkit.WebView.<init> (WebView.java:574)
at android.webkit.WebView.<init> (WebView.java:564)
at com.original.works.OriginalWorkApplication.initializeWebView (OriginalWorkApplication.java:56)
at com.original.works.OriginalWorkApplication.onCreate (OriginalWorkApplication.java:22)
source to share