nowinandroid icon indicating copy to clipboard operation
nowinandroid copied to clipboard

Sync dark theme preference to UiModeManager for splash screen

Open stevenelliottjr opened this issue 10 months ago • 3 comments

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

  • [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)

stevenelliottjr avatar Feb 03 '26 17:02 stevenelliottjr

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.

google-cla[bot] avatar Feb 03 '26 17:02 google-cla[bot]

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 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 :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.

gemini-code-assist avatar Feb 03 '26 17:02 gemini-code-assist

@googlebot I signed it

stevenelliottjr avatar Feb 03 '26 17:02 stevenelliottjr