admin管理员组

文章数量:1379395

I am experiencing an issue in my Android app related to using WebView in a fragment while forcing a theme mode opposite to the system's current mode. When the system is in dark mode, I force light mode in my app, and after opening a WebView, the UI components mix between light and dark modes when the WebView is closed. Specifically, I am using setWebViewClient to load a URL, and I've tried controlling WebView settings with forceDark = WebSettings.FORCE_DARK_OFF, but the problem persists.

Steps to Reproduce: 1- The system is in dark mode. 2- I force light mode using the app (via AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)). 3- Open a WebView (with a custom URL) inside a fragment. 4- After the WebView is closed, the app UI components have mixed resources—some appear in dark mode and others in light mode.

What I’ve Tried: 1- Using forceDark = WebSettings.FORCE_DARK_OFF in WebView settings. 2- Calling invalidate() on the decorView. 3= Calling recreate() on the activity, but that resets the state of the app, which I don’t want. 4- Forcing light mode using AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO).

Is there a way to force the UI to stay in light mode (or the chosen mode) after interacting with WebView, without causing mixed resources? What is the best approach to ensure the app doesn’t reset its state while handling theme changes correctly?

本文标签: