Sync dark theme preference to UiModeManager for splash screen
Summary
- Syncs the user's
DarkThemeConfigpreference toUiModeManager.setApplicationNightMode()so the splash screen uses the correct theme on the next cold start - Without this fix, selecting "Dark" in the app settings still shows a light splash screen on relaunch, causing a visual flash
- Guarded behind
Build.VERSION_CODES.S(API 31+) sincesetApplicationNightModewas introduced there
Details
On cold start, the Android splash screen theme is determined by the system-level UiModeManager night mode, not by the in-app Compose theme. The app already correctly applies the user's dark theme preference to edge-to-edge styling and NiaTheme, but never communicated this preference back to the system.
This adds a lifecycle-aware Flow collector in MainActivity.onCreate() that:
- Observes
MainActivityUiStatefor the user'sDarkThemeConfig - Maps it to the corresponding
UiModeManagernight mode constant - Calls
setApplicationNightMode()to keep the system in sync
Uses distinctUntilChanged() to avoid redundant system calls and mapNotNull to skip the Loading state.
Test plan
- [x] Build succeeds (
./gradlew :app:installDemoDebug) - [x] App launches without crashes on API 36 emulator
- [x] No logcat errors related to
UiModeManager - [ ] Manual: Set theme to Dark → force stop → cold launch → splash screen should be dark
- [ ] Manual: Set theme to Light → force stop → cold launch → splash screen should be light
- [ ] Manual: Set theme to Follow System → verify splash follows system setting
- [ ] Verify no behavior change on API < 31 (code is guarded)
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 Changes
Hello @stevenelliottjr, I'm Gemini Code Assist[^1]! 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
- Splash Screen Theming Fix: Addresses an issue where the Android splash screen would incorrectly display a light theme on cold starts, even when the user had selected a dark theme within the app, leading to a visual flash.
- System UI Mode Synchronization: Introduces logic to synchronize the user's in-app
DarkThemeConfigpreference with the system-levelUiModeManager.setApplicationNightMode()to ensure the splash screen respects the chosen theme. - API Level Guard: The new synchronization mechanism is guarded to only apply on Android API 31 (Android S) and above, as
setApplicationNightModewas introduced in this version. - Lifecycle-Aware Flow Collection: Implements a lifecycle-aware
Flowcollector withinMainActivity.onCreate()to observeMainActivityUiStateforDarkThemeConfigchanges and apply them to theUiModeManager.
🧠 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
- app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt
- Added imports for
android.app.UiModeManager,android.os.Build,MainActivityUiState.Success,DarkThemeConfig, andkotlinx.coroutines.flow.mapNotNull. - Implemented a new
lifecycleScope.launchblock withinonCreateto observe theviewModel.uiStatefordarkThemeConfigchanges. - Mapped the
DarkThemeConfigvalues (FOLLOW_SYSTEM, LIGHT, DARK) to their correspondingUiModeManagernight mode constants (MODE_NIGHT_AUTO, MODE_NIGHT_NO, MODE_NIGHT_YES). - Utilized
distinctUntilChanged()to prevent redundant system calls when the theme preference has not changed. - Called
uiModeManager.setApplicationNightMode()to update the system's night mode setting. - Wrapped the entire synchronization logic in a conditional check for
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Sto ensure compatibility with older Android versions.
- Added imports for
Activity
- The pull request was created by stevenelliottjr.
- The author has provided a detailed summary, implementation details, and a comprehensive test plan in the pull request description.
- No human review comments or additional activity have been recorded yet.
Using Gemini Code Assist
The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.
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 :thumbsup: and :thumbsdown: 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.
[^1]: Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.
@googlebot I signed it