isSystemInDarkTheme

Observes system dark theme changes as a reactive Flow.

This function creates a cold Flow that emits the current dark theme state immediately upon collection, then emits again whenever the system theme changes. The Flow automatically handles listener registration/unregistration and filters out duplicate consecutive values.

Usage in ViewModel

@HiltViewModel
class ThemeViewModel @Inject constructor() : ViewModel() {
// This won't work directly - need to pass Activity or use a different approach
// Typically you'd observe this from the Activity and update preferences
}

Usage in Activity

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

lifecycleScope.launch {
isSystemInDarkTheme().collect { isDark ->
Log.d("Theme", "System dark mode: $isDark")
// Update theme settings
}
}
}
}

Usage in Composable

@Composable
fun ThemeAwareContent() {
val context = LocalContext.current
val activity = context.getActivity() as? ComponentActivity

val isDarkTheme by activity?.isSystemInDarkTheme()
?.collectAsState(initial = false)
?: remember { mutableStateOf(false) }

// Use isDarkTheme...
}

Flow Characteristics

  • Cold Flow: Listener is registered only when collected

  • Distinct: Filters duplicate consecutive values

  • Conflated: Only keeps the latest value if collector is slow

  • Lifecycle-aware: Must be collected with appropriate scope

Return

Cold Flow that emits true when system is in dark mode, false otherwise

See also


Extension property to check if the system is currently in dark theme mode.

This property provides a convenient way to check the system's dark mode setting from a Configuration object.

Usage

val isDark = resources.configuration.isSystemInDarkTheme
if (isDark) {
// Apply dark theme specific logic
}

Return

true if system is in dark mode, false otherwise

See also