Skip to content

Sync dark theme preference to UiModeManager for splash screen#2060

Open
stevenelliottjr wants to merge 2 commits intoandroid:mainfrom
stevenelliottjr:fix/splashscreen-theme-sync
Open

Sync dark theme preference to UiModeManager for splash screen#2060
stevenelliottjr wants to merge 2 commits intoandroid:mainfrom
stevenelliottjr:fix/splashscreen-theme-sync

Conversation

@stevenelliottjr
Copy link

Summary

  • Syncs the user's DarkThemeConfig preference to UiModeManager.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+) since setApplicationNightMode was 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:

  1. Observes MainActivityUiState for the user's DarkThemeConfig
  2. Maps it to the corresponding UiModeManager night mode constant
  3. Calls setApplicationNightMode() to keep the system in sync

Uses distinctUntilChanged() to avoid redundant system calls and mapNotNull to skip the Loading state.

Test plan

  • Build succeeds (./gradlew :app:installDemoDebug)
  • App launches without crashes on API 36 emulator
  • 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)

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.
@google-cla
Copy link

google-cla bot commented Feb 3, 2026

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.

@gemini-code-assist
Copy link

Summary of Changes

Hello @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

  • 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 DarkThemeConfig preference with the system-level UiModeManager.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 setApplicationNightMode was introduced in this version.
  • Lifecycle-Aware Flow Collection: Implements a lifecycle-aware Flow collector within MainActivity.onCreate() to observe MainActivityUiState for DarkThemeConfig changes and apply them to the UiModeManager.

🧠 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, and kotlinx.coroutines.flow.mapNotNull.
    • Implemented a new lifecycleScope.launch block within onCreate to observe the viewModel.uiState for darkThemeConfig changes.
    • Mapped the DarkThemeConfig values (FOLLOW_SYSTEM, LIGHT, DARK) to their corresponding UiModeManager night 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.S to ensure compatibility with older Android versions.
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 👍 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

  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.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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.

Comment on lines 137 to 155
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
},
)
}
}
}
}

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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
},
)
}
}
}
}

@stevenelliottjr
Copy link
Author

@googlebot I signed it

Move getSystemService call outside the flow collector so
the system service is resolved once rather than on every emission.
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