Sync dark theme preference to UiModeManager for splash screen#2060
Sync dark theme preference to UiModeManager for splash screen#2060stevenelliottjr wants to merge 2 commits intoandroid:mainfrom
Conversation
On cold start, the splash screen theme is determined by the application's UiModeManager night mode setting, not by the in-app Compose theme. Without this change, a user who selects "Dark" in the app's settings will still see a light splash screen on the next launch, causing a jarring visual flash. This adds a lifecycle-aware collector that maps the user's DarkThemeConfig preference to UiModeManager.setApplicationNightMode(), ensuring the splash screen matches the chosen theme. Guarded behind API 31+ (Android S) since setApplicationNightMode was introduced there.
|
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. |
Summary of ChangesHello @stevenelliottjr, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the user experience by ensuring a consistent theme from the very first moment an app launches. It resolves a visual inconsistency where the splash screen might not reflect the user's preferred dark theme, by actively communicating the app's theme setting to the Android system's UI mode manager. This integration provides a smoother and more polished cold start experience for users on modern Android versions. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly syncs the user's dark theme preference to UiModeManager to ensure the splash screen has the correct theme on cold starts. The implementation is clean and follows best practices for Android development. I have one suggestion to improve performance by hoisting the getSystemService call out of the collect block.
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | ||
| lifecycleScope.launch { | ||
| lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
| viewModel.uiState | ||
| .mapNotNull { (it as? Success)?.userData?.darkThemeConfig } | ||
| .distinctUntilChanged() | ||
| .collect { darkThemeConfig -> | ||
| val uiModeManager = getSystemService(UiModeManager::class.java) | ||
| uiModeManager.setApplicationNightMode( | ||
| when (darkThemeConfig) { | ||
| DarkThemeConfig.FOLLOW_SYSTEM -> UiModeManager.MODE_NIGHT_AUTO | ||
| DarkThemeConfig.LIGHT -> UiModeManager.MODE_NIGHT_NO | ||
| DarkThemeConfig.DARK -> UiModeManager.MODE_NIGHT_YES | ||
| }, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
To improve performance, it's better to get the UiModeManager instance once and reuse it, rather than calling getSystemService on every emission of the darkThemeConfig flow. You can move the getSystemService call outside the collect block.
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | |
| lifecycleScope.launch { | |
| lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { | |
| viewModel.uiState | |
| .mapNotNull { (it as? Success)?.userData?.darkThemeConfig } | |
| .distinctUntilChanged() | |
| .collect { darkThemeConfig -> | |
| val uiModeManager = getSystemService(UiModeManager::class.java) | |
| uiModeManager.setApplicationNightMode( | |
| when (darkThemeConfig) { | |
| DarkThemeConfig.FOLLOW_SYSTEM -> UiModeManager.MODE_NIGHT_AUTO | |
| DarkThemeConfig.LIGHT -> UiModeManager.MODE_NIGHT_NO | |
| DarkThemeConfig.DARK -> UiModeManager.MODE_NIGHT_YES | |
| }, | |
| ) | |
| } | |
| } | |
| } | |
| } | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | |
| val uiModeManager = getSystemService(UiModeManager::class.java) | |
| lifecycleScope.launch { | |
| lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { | |
| viewModel.uiState | |
| .mapNotNull { (it as? Success)?.userData?.darkThemeConfig } | |
| .distinctUntilChanged() | |
| .collect { darkThemeConfig -> | |
| uiModeManager.setApplicationNightMode( | |
| when (darkThemeConfig) { | |
| DarkThemeConfig.FOLLOW_SYSTEM -> UiModeManager.MODE_NIGHT_AUTO | |
| DarkThemeConfig.LIGHT -> UiModeManager.MODE_NIGHT_NO | |
| DarkThemeConfig.DARK -> UiModeManager.MODE_NIGHT_YES | |
| }, | |
| ) | |
| } | |
| } | |
| } | |
| } |
|
@googlebot I signed it |
Move getSystemService call outside the flow collector so the system service is resolved once rather than on every emission.
Summary
DarkThemeConfigpreference toUiModeManager.setApplicationNightMode()so the splash screen uses the correct theme on the next cold startBuild.VERSION_CODES.S(API 31+) sincesetApplicationNightModewas introduced thereDetails
On cold start, the Android splash screen theme is determined by the system-level
UiModeManagernight mode, not by the in-app Compose theme. The app already correctly applies the user's dark theme preference to edge-to-edge styling andNiaTheme, but never communicated this preference back to the system.This adds a lifecycle-aware
Flowcollector inMainActivity.onCreate()that:MainActivityUiStatefor the user'sDarkThemeConfigUiModeManagernight mode constantsetApplicationNightMode()to keep the system in syncUses
distinctUntilChanged()to avoid redundant system calls andmapNotNullto skip theLoadingstate.Test plan
./gradlew :app:installDemoDebug)UiModeManager