LocalAmbientModeManager

Property
Android
public val LocalAmbientModeManager: ProvidableCompositionLocal<AmbientModeManager?>

A androidx.compose.runtime.CompositionLocal that provides the current AmbientModeManager interface.

This local is the primary way for any composable in the hierarchy to access the manager responsible for tracking the ambient (low-power) mode state.

Composables read the manager using LocalAmbientModeManager.current and subscribe to changes via the AmbientModeManager.currentAmbientMode property.

If no AmbientModeManager is explicitly provided higher up the tree (e.g., via rememberAmbientModeManager), this local returns null.

Code Examples

AmbientModeBasicSample

@Composable
fun AmbientModeBasicSample() {
    // **Best Practice Note:** In a production application, the AmbientModeManager should be
    // instantiated and provided at the highest level of the Compose hierarchy (typically in
    // the host Activity's setContent block) using a CompositionLocalProvider. This ensures
    // proper lifecycle management and broad accessibility.
    // For this self-contained demo, AmbientModeManager is created and provided locally:
    val activityAmbientModeManager = rememberAmbientModeManager()
    CompositionLocalProvider(LocalAmbientModeManager provides activityAmbientModeManager) {
        val ambientModeManager = LocalAmbientModeManager.current
        val ambientMode = ambientModeManager?.currentAmbientMode
        Column(
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.fillMaxSize(),
        ) {
            val ambientModeName =
                when (ambientMode) {
                    is AmbientMode.Interactive -> "Interactive"
                    is AmbientMode.Ambient -> "Ambient"
                    else -> "Unknown"
                }
            val color = if (ambientMode is AmbientMode.Ambient) Color.Gray else Color.Yellow
            Text(text = "$ambientModeName Mode", color = color)
        }
    }
}