Skip to content

Commit

Permalink
add lifecycle param
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke committed Dec 9, 2024
1 parent 6425377 commit 2d65d11
Showing 1 changed file with 3 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.wear.ambient.AmbientLifecycleObserver

Expand All @@ -42,17 +43,18 @@ import androidx.wear.ambient.AmbientLifecycleObserver
*
* It should be used within each individual screen inside nav routes.
*
* @param lifecycle The [Lifecycle] of the activity or current owner such as NavBackStackEntry.
* @param content Lambda that will be used for building the UI, which is passed the current ambient
* state.
*/
@Composable
fun AmbientAware(
lifecycle: Lifecycle = LocalLifecycleOwner.current.lifecycle,
content: @Composable (AmbientState) -> Unit,
) {
// Using AmbientAware correctly relies on there being an Activity context. If there isn't, then
// gracefully allow the composition of [block], but no ambient-mode functionality is enabled.
val activity = LocalContext.current.findActivityOrNull()
val lifecycle = LocalLifecycleOwner.current.lifecycle

val ambientState = rememberAmbientState(activity, lifecycle)

Expand Down

0 comments on commit 2d65d11

Please sign in to comment.