Skip to content

Conversation

@300Ech
Copy link

@300Ech 300Ech commented Dec 5, 2025

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

  • Initialize night mode in NiaApplication.onCreate() using runBlocking to ensure it completes before the splash screen is displayed
  • Observe theme preference changes to immediately notify when the user changes the theme, ensuring subsequent cold starts use the correct theme
  • Use UiModeManager.setApplicationNightMode() on API 31+, fall back to AppCompatDelegate on older Android versions
  • Add extension functions to UiExtensions.kt

Why 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

  • ✅ Set device to Light mode, app to Dark theme → Splash screen shows dark
  • ✅ Set device to Dark mode, app to Light theme → Splash screen shows light
  • ✅ Set app to Follow System → Splash screen matches system theme
  • ✅ Tested on Android 16 (API 36) and Android 11 (API 30) devices
  • ✅ All existing tests pass

Checklist

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.
@300Ech 300Ech requested a review from dturner as a code owner December 5, 2025 03:50
@google-cla
Copy link

google-cla bot commented Dec 5, 2025

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.

@seve-andre
Copy link
Contributor

For reference, I implemented and recently updated a similar non-blocking solution (see #1718).
Hopefully one of these approaches can be merged soon.

@300Ech
Copy link
Author

300Ech commented Dec 5, 2025

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!

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.

[Bug]: Splashscreen theme doesn't change after switching the color theme

2 participants