rememberAmbientModeManager

Composable Function
Android
@Composable
public fun rememberAmbientModeManager(): AmbientModeManager

Creates, remembers, and manages the lifecycle of the default AmbientModeManager implementation.

**This function requires a LocalActivity be present and automatically enables Always-On mode for that activity, ensuring it remains visible in the low-power ambient state.**

The resulting AmbientModeManager is retained across recompositions via remember and its system listeners are automatically registered and unregistered using DisposableEffect, tying the ambient tracking to the composition lifecycle.

See the Android documentation for more details on enabling Always-On mode: https://developer.android.com/training/wearables/views/always-on

Example of a simple screen switching between Interactive and Ambient modes:

Returns

A new or remembered AmbientModeManager instance.

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