From 302648afde4592e02aa090c49d4c4ca04179c2a2 Mon Sep 17 00:00:00 2001 From: shaominngqing Date: Tue, 3 Feb 2026 19:18:26 +0800 Subject: [PATCH] Fix splashscreen theme not updating after theme change Apply AppCompatDelegate.setDefaultNightMode() when the user changes the dark theme configuration to ensure the system-level night mode is synchronized. This makes the splash screen display with the correct theme on cold start. Changes: - SettingsViewModel: Apply night mode immediately when user changes theme - MainActivity: Apply saved theme config on startup Fixes #633 --- .../samples/apps/nowinandroid/MainActivity.kt | 20 +++++++++++++++++++ .../settings/impl/SettingsViewModel.kt | 6 ++++++ 2 files changed, 26 insertions(+) diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt index ecc23d80e..90f842d0e 100644 --- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt +++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt @@ -22,6 +22,7 @@ import androidx.activity.SystemBarStyle import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.activity.viewModels +import androidx.appcompat.app.AppCompatDelegate import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -34,6 +35,7 @@ import androidx.lifecycle.repeatOnLifecycle import androidx.metrics.performance.JankStats import androidx.tracing.trace import com.google.samples.apps.nowinandroid.MainActivityUiState.Loading +import com.google.samples.apps.nowinandroid.MainActivityUiState.Success import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsHelper import com.google.samples.apps.nowinandroid.core.analytics.LocalAnalyticsHelper import com.google.samples.apps.nowinandroid.core.data.repository.UserNewsResourceRepository @@ -44,6 +46,7 @@ import com.google.samples.apps.nowinandroid.core.ui.LocalTimeZone import com.google.samples.apps.nowinandroid.ui.NiaApp import com.google.samples.apps.nowinandroid.ui.rememberNiaAppState import com.google.samples.apps.nowinandroid.util.isSystemInDarkTheme +import com.google.samples.apps.nowinandroid.util.toNightMode import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged @@ -79,6 +82,23 @@ class MainActivity : ComponentActivity() { val splashScreen = installSplashScreen() super.onCreate(savedInstanceState) + // Apply the saved dark theme config at startup to ensure the splash screen + // uses the correct theme. This addresses issue #633. + lifecycleScope.launch { + viewModel.uiState + .map { state -> + if (state is Success) { + state.userData.darkThemeConfig.toNightMode() + } else { + null + } + } + .first { it != null } // Only take the first non-null mode + .let { mode -> + AppCompatDelegate.setDefaultNightMode(mode) + } + } + // We keep this as a mutable state, so that we can track changes inside the composition. // This allows us to react to dark/light mode changes. var themeSettings by mutableStateOf( diff --git a/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/SettingsViewModel.kt b/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/SettingsViewModel.kt index 274f916d1..ddb7df2bf 100644 --- a/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/SettingsViewModel.kt +++ b/feature/settings/impl/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/settings/impl/SettingsViewModel.kt @@ -16,6 +16,7 @@ package com.google.samples.apps.nowinandroid.feature.settings.impl +import androidx.appcompat.app.AppCompatDelegate import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository @@ -62,6 +63,11 @@ class SettingsViewModel @Inject constructor( fun updateDarkThemeConfig(darkThemeConfig: DarkThemeConfig) { viewModelScope.launch { userDataRepository.setDarkThemeConfig(darkThemeConfig) + + // Apply the night mode at the application level to ensure the splash screen + // uses the correct theme on cold start. This addresses issue #633. + // Reference: https://developer.android.com/develop/ui/views/theming/darktheme#change-themes + AppCompatDelegate.setDefaultNightMode(darkThemeConfig.toNightMode()) } }