Skip to content

Fix: prevent WebView reset on dark/light mode switch - #136

Merged
Android-PowerUser merged 1 commit into
mainfrom
human-operator
Jul 23, 2026
Merged

Fix: prevent WebView reset on dark/light mode switch#136
Android-PowerUser merged 1 commit into
mainfrom
human-operator

Conversation

@Android-PowerUser

Copy link
Copy Markdown
Owner

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.

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.
@Android-PowerUser
Android-PowerUser merged commit 801cedb into main Jul 23, 2026
8 of 9 checks passed
@Android-PowerUser
Android-PowerUser deleted the human-operator branch July 23, 2026 07:08

@amazon-q-developer amazon-q-developer Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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">

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛑 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
            )
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant