From 8b7b4f1a0b674b38d0e92a78db96e395202fd5de Mon Sep 17 00:00:00 2001 From: Steven Elliott Date: Tue, 3 Feb 2026 13:43:13 -0500 Subject: [PATCH] Hoist UiModeManager lookup out of collect block Move getSystemService call outside the flow collector so the system service is resolved once rather than on every emission. --- .../kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 cdb52385c..0e74c54dc 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 @@ -135,13 +135,13 @@ class MainActivity : ComponentActivity() { // Sync the user's dark theme preference to the system-level UiModeManager so the // splash screen uses the correct theme on the next cold start. 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 -> - val uiModeManager = getSystemService(UiModeManager::class.java) uiModeManager.setApplicationNightMode( when (darkThemeConfig) { DarkThemeConfig.FOLLOW_SYSTEM -> UiModeManager.MODE_NIGHT_AUTO