12

I use Theme.AppCompat.DayNight.NoActionBar theme for my app. When I load adMob interstital some colors are broken in "night" mode (i.e. in RecyclerView).

Screen:

enter image description here

Those incorrect colors are from "notnight" values. When I close the app and run it again everything is okay. When I kill the app 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 ok.

You can find whole project here: github

Mateusz Kaflowski
  • 2,221
  • 1
  • 29
  • 35

1 Answers1

23

The issue is supposedly caused by the WebView resetting the UI mode, and this can be worked around by instantiating WebView manually.

I haven't seen the issue after doing this (in Application.oncreate() in this particular app):

    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

Roy Solberg
  • 18,133
  • 12
  • 49
  • 76
  • 1
    WOW, Thank you so much, that worked! I´ve searched a long while, but without any success. I think your post and the discussion in the google group forum is the only one about this (VERY CRITICAL and DIFFICULT) topic/bug. – JonasPTFL Jun 18 '19 at 20:11
  • 2
    This is no longer an issue and no need to continue relying on the work around. Google has fixed the issue and we just have to update our appcompat to a minimum of https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0-alpha03 – display name Aug 29 '19 at 18:28
  • 4
    I had the same issue when I create a webview component after setting dark mode. Even though I updated Appcompat to 1.1.0-rc01, the issue was still there. The above work around fixed the issue. – Salmon K P Aug 30 '19 at 10:16
  • 1
    For anyone put the code in `Application.oncreate()` don't work like me try to put it in `Activity.onCreate()` – Kyo Huu Jun 25 '20 at 08:59
  • webview init inside application class, will affect application init performance. it will took about 200ms. – changhao cui Jan 28 '23 at 04:43
  • @changhaocui: That's a very good point. It's a big penalty. I've since moved it to the main screen to at least not affect the init performance of incoming push messages and widget updates (i.e. main screen with ads won't load). – Roy Solberg Jan 28 '23 at 13:10