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
pull/2058/head
shaominngqing 4 weeks ago
parent 5ba16edde9
commit 302648afde

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

@ -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())
}
}

Loading…
Cancel
Save