Fix splash screen theme not respecting user preference on Android 12+#2012
Open
300Ech wants to merge 1 commit intoandroid:mainfrom
Open
Fix splash screen theme not respecting user preference on Android 12+#2012300Ech wants to merge 1 commit intoandroid:mainfrom
300Ech wants to merge 1 commit intoandroid:mainfrom
Conversation
Fixes android#633 On Android 12 and above, the splash screen was ignoring the user’s saved theme preference and instead defaulting to the system theme. This happened because the splash screen is rendered before Application.onCreate() finishes executing. At that point, the system’s UiModeManager hadn't been updated with the user's preferred theme, so it showed the wrong one. Changes: - Introduced initializeNightModeFromPreferences() in UiExtensions.kt. This method forces the correct theme mode synchronously during Application.onCreate(), ensuring it’s in place before the splash screen ever appears. - Added observeNightModePreferences() to actively listen for changes to the theme setting. This keeps UiModeManager updated in real time so future cold starts use the right theme immediately. - On Android 12+ (API 31+), now using UiModeManager.setApplicationNightMode() to set the theme properly. - For older versions, the implementation falls back to AppCompatDelegate.setDefaultNightMode() for compatibility. - Added a toDarkThemeConfig() extension function to translate the DataStore proto into the theme model used by the UI. - Included the AppCompat dependency in the app module to support these changes. Why the Observer Matters? Without it, any theme change made by the user wouldn't notify UiModeManager right away. That would mean the next time the app is launched cold, the splash screen would still use whatever theme was cached before—not the one the user just selected. This fix prevents that. Testing: - Confirmed that the splash screen reflects the saved theme preference, even on a fresh app start. - Checked all theme configurations: Light, Dark, and Follow System. - Verified behavior on both Android 12+ and Android 11 devices to ensure backward compatibility.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Contributor
|
For reference, I implemented and recently updated a similar non-blocking solution (see #1718). |
Author
|
hi! thanks for pointing that out @seve-andre, appreciate it! Happy to leave it to the maintainers to decide what fits best, or if both ideas can be combined. Either way, glad to see this getting addressed! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #633
The splash screen was displaying the system theme instead of respecting the user's saved app theme preference on Android 12+ devices.
Problem
On Android 12+, the system shows the splash screen before Application.onCreate() finishes. The app saved the theme setting, but UiModeManager wasn’t updated, so the splash screen ignored the user's theme.
Solution
NiaApplication.onCreate()usingrunBlockingto ensure it completes before the splash screen is displayedUiModeManager.setApplicationNightMode()on API 31+, fall back toAppCompatDelegateon older Android versionsWhy the Observer is Needed
After a theme change, the first cold start shows the wrong splash screen since UiModeManager isn’t updated right away. Watching for changes fixes that instantly.
Testing
Checklist