# AmbientLifecycleObserverKt

Added in 1.3.0

*** ** * ** ***


```
public final class AmbientLifecycleObserverKt
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/wear/ambient/AmbientLifecycleObserver` | `https://developer.android.com/reference/androidx/wear/ambient/AmbientLifecycleObserverKt#AmbientLifecycleObserver(android.app.Activity,androidx.wear.ambient.AmbientLifecycleObserver.AmbientLifecycleCallback)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/Activity.html activity, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/wear/ambient/AmbientLifecycleObserver.AmbientLifecycleCallback callbacks )` Create a new `https://developer.android.com/reference/androidx/wear/ambient/package-summary#AmbientLifecycleObserver(android.app.Activity,androidx.wear.ambient.AmbientLifecycleObserver.AmbientLifecycleCallback)` for use on a real device. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/wear/ambient/AmbientLifecycleObserver` | `https://developer.android.com/reference/androidx/wear/ambient/AmbientLifecycleObserverKt#AmbientLifecycleObserver(android.app.Activity,java.util.concurrent.Executor,androidx.wear.ambient.AmbientLifecycleObserver.AmbientLifecycleCallback)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/Activity.html activity, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/Executor.html callbackExecutor, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/wear/ambient/AmbientLifecycleObserver.AmbientLifecycleCallback callbacks )` Create a new `https://developer.android.com/reference/androidx/wear/ambient/package-summary#AmbientLifecycleObserver(android.app.Activity,java.util.concurrent.Executor,androidx.wear.ambient.AmbientLifecycleObserver.AmbientLifecycleCallback)` for use on a real device. |

## Public methods

### AmbientLifecycleObserver

Artifact: [androidx.wear:wear](https://developer.android.com/jetpack/androidx/releases/wear) [View Source](https://cs.android.com/search?q=file:androidx/wear/ambient/AmbientLifecycleObserver.kt+function:AmbientLifecycleObserver) Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/wear#1.3.0)

```
public static final @NonNull AmbientLifecycleObserver AmbientLifecycleObserver(
    @NonNull Activity activity,
    @NonNull AmbientLifecycleObserver.AmbientLifecycleCallback callbacks
)
```

Create a new `https://developer.android.com/reference/androidx/wear/ambient/package-summary#AmbientLifecycleObserver(android.app.Activity,androidx.wear.ambient.AmbientLifecycleObserver.AmbientLifecycleCallback)` for use on a real device.

Applications which wish to show layouts in ambient mode should attach the returned observer to their activities or fragments, passing in a set of callback to be notified about ambient state. In addition, the app needs to declare that it uses the `https://developer.android.com/reference/android/Manifest.permission.html#WAKE_LOCK` permission in its manifest.

The created `https://developer.android.com/reference/androidx/wear/ambient/package-summary#AmbientLifecycleObserver(android.app.Activity,androidx.wear.ambient.AmbientLifecycleObserver.AmbientLifecycleCallback)` can also be used to query whether the device is in ambient mode.

As an example of how to use this class, see the following example:

```
class MyActivity : ComponentActivity() {
    private val callbacks = object : AmbientLifecycleObserver.AmbientLifecycleCallback {
        // ...
    }

    private val ambientObserver = AmbientLifecycleObserver(this, callbacks)

    override fun onCreate(savedInstanceState: Bundle) {
        super.onCreate(savedInstanceState)
        lifecycle.addObserver(ambientObserver)
    }
}
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/Activity.html activity` | The activity that this observer is being attached to. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/wear/ambient/AmbientLifecycleObserver.AmbientLifecycleCallback callbacks` | An instance of `https://developer.android.com/reference/androidx/wear/ambient/AmbientLifecycleObserver.AmbientLifecycleCallback`, used to notify the observer about changes to the ambient state. |

### AmbientLifecycleObserver

Artifact: [androidx.wear:wear](https://developer.android.com/jetpack/androidx/releases/wear) [View Source](https://cs.android.com/search?q=file:androidx/wear/ambient/AmbientLifecycleObserver.kt+function:AmbientLifecycleObserver) Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/wear#1.3.0)

```
public static final @NonNull AmbientLifecycleObserver AmbientLifecycleObserver(
    @NonNull Activity activity,
    @NonNull Executor callbackExecutor,
    @NonNull AmbientLifecycleObserver.AmbientLifecycleCallback callbacks
)
```

Create a new `https://developer.android.com/reference/androidx/wear/ambient/package-summary#AmbientLifecycleObserver(android.app.Activity,java.util.concurrent.Executor,androidx.wear.ambient.AmbientLifecycleObserver.AmbientLifecycleCallback)` for use on a real device.

Applications which wish to show layouts in ambient mode should attach the returned observer to their activities or fragments, passing in a set of callback to be notified about ambient state. In addition, the app needs to declare that it uses the `https://developer.android.com/reference/android/Manifest.permission.html#WAKE_LOCK` permission in its manifest.

The created `https://developer.android.com/reference/androidx/wear/ambient/package-summary#AmbientLifecycleObserver(android.app.Activity,java.util.concurrent.Executor,androidx.wear.ambient.AmbientLifecycleObserver.AmbientLifecycleCallback)` can also be used to query whether the device is in ambient mode.

As an example of how to use this class, see the following example:

```
class MyActivity : ComponentActivity() {
    private val callbacks = object : AmbientLifecycleObserver.AmbientLifecycleCallback {
        // ...
    }

    private val ambientObserver = AmbientLifecycleObserver(this, mainExecutor, callbacks)

    override fun onCreate(savedInstanceState: Bundle) {
        super.onCreate(savedInstanceState)
        lifecycle.addObserver(ambientObserver)
    }
}
```

If the observer is registered while the device is in ambient mode, the registered callback will immediately receive a call to `https://developer.android.com/reference/androidx/wear/ambient/AmbientLifecycleObserver.AmbientLifecycleCallback#onEnterAmbient(androidx.wear.ambient.AmbientLifecycleObserver.AmbientDetails)`. If the device is in active mode, the callbacks will be registered, and `onEnterAmbient` will be called when the device next enters ambient mode.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/Activity.html activity` | The activity that this observer is being attached to. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/Executor.html callbackExecutor` | The executor to run the provided callbacks on. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/wear/ambient/AmbientLifecycleObserver.AmbientLifecycleCallback callbacks` | An instance of `https://developer.android.com/reference/androidx/wear/ambient/AmbientLifecycleObserver.AmbientLifecycleCallback`, used to notify the observer about changes to the ambient state. |