Fix: prevent WebView reset on dark/light mode switch - #136
Conversation
Added android:configChanges=uiMode (plus orientation/screenSize/ screenLayout/keyboardHidden) to MainActivity so the Activity is not recreated when the system switches between dark and light theme. Previously the activity restarted on theme change, causing onCreate to refetch and reload the remote HTML into the WebView, which reset navigation back to the main page.
There was a problem hiding this comment.
This PR adds android:configChanges to prevent Activity recreation on dark/light mode switches, which would preserve WebView navigation state. However, there is a critical implementation issue that must be addressed:
Critical Finding:
The AndroidManifest change requires implementing onConfigurationChanged() in MainActivity.kt to handle theme changes. Without this callback, the WebView UI will not update to match the new system theme when users switch between dark and light modes.
The fix requires adding the onConfigurationChanged() callback to MainActivity.kt to update WebView dark mode settings when configuration changes occur. See the inline comment for the specific implementation needed.
You can now have the agent implement changes and create commits directly on your pull request's source branch. Simply comment with /q followed by your request in natural language to ask the agent to make changes.
| android:launchMode="singleTop" | ||
| android:windowSoftInputMode="adjustResize"> | ||
| android:windowSoftInputMode="adjustResize" | ||
| android:configChanges="uiMode|orientation|screenSize|screenLayout|keyboardHidden"> |
There was a problem hiding this comment.
🛑 Logic Error: Adding android:configChanges="uiMode|orientation|screenSize|screenLayout|keyboardHidden" prevents Activity recreation, but MainActivity.kt does not implement the required onConfigurationChanged() callback. Without this callback, the app cannot respond to theme changes, causing the WebView UI to remain in the old theme after switching between dark/light mode. The WebView dark mode logic (lines 841-854 in MainActivity.kt) only executes during WebView factory creation in onCreate(), which no longer runs on theme changes with this configuration.
Add this callback to MainActivity.kt to handle configuration changes:
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
// Update WebView dark mode settings
webViewInstance?.let { webView ->
if (WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) {
WebSettingsCompat.setAlgorithmicDarkeningAllowed(webView.settings, true)
} else if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
val isNightMode = (newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
@Suppress("DEPRECATION")
WebSettingsCompat.setForceDark(
webView.settings,
if (isNightMode) WebSettingsCompat.FORCE_DARK_ON else WebSettingsCompat.FORCE_DARK_OFF
)
}
}
}
Added android:configChanges=uiMode (plus orientation/screenSize/ screenLayout/keyboardHidden) to MainActivity so the Activity is not recreated when the system switches between dark and light theme. Previously the activity restarted on theme change, causing onCreate to refetch and reload the remote HTML into the WebView, which reset navigation back to the main page.