Solving java.lang.IllegalStateException: CompositionLocal LocalLifecycleOwner Not Present

Nemat
2 min readMay 25, 2024

--

Have you suddenly started getting this error? I did. I wrote a very simple code where I wanted to use `collectAsStateWithLifecycle` in my Compose setup, but it kept giving me this error:


java.lang.IllegalStateException: CompositionLocal LocalLifecycleOwner not present
at androidx.lifecycle.compose.LocalLifecycleOwnerKt$LocalLifecycleOwner$1.invoke(LocalLifecycleOwner.kt:26)
at androidx.lifecycle.compose.LocalLifecycleOwnerKt$LocalLifecycleOwner$1.invoke(LocalLifecycleOwner.kt:25)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at androidx.compose.runtime.LazyValueHolder.getCurrent(ValueHolders.kt:29)
at androidx.compose.runtime.LazyValueHolder.getValue(ValueHolders.kt:31)
at androidx.compose.runtime.CompositionLocalMapKt.read(CompositionLocalMap.kt:90)
at androidx.compose.runtime.ComposerImpl.consume(Composer.kt:2135)
at androidx.lifecycle.compose.FlowExtKt.collectAsStateWithLifecycle(FlowExt.kt:182)

This issue is already reported in the issue tracker here. There’s also a Stack Overflow thread discussing this problem.

Understanding the Error

According to this, this issue occurs due to a mismatch between Compose and Lifecycle versions. Specifically, Lifecycle 2.8.0 is incompatible with the stable Compose versions. Once Compose 1.7 Beta becomes stable, this error will likely be resolved.

Solutions

There are three suggested workarounds to fix this issue, as noted in the issue tracker:

1. Use Compose 1.7 Beta

Updating to Compose 1.7 Beta can resolve the incompatibility. This is the most straightforward solution, but it involves using a beta version, which might not be suitable for all projects.

2. Map `LocalLifecycleOwner`

Map `androidx.compose.ui.platform.LocalLifecycleOwner` (from Compose 1.6) to `androidx.lifecycle.compose.LocalLifecycleOwner` (from Lifecycle 2.8). Here’s an example mentioned in the issue tracker’s thread:


CompositionLocalProvider(
androidx.lifecycle.compose.LocalLifecycleOwner provides androidx.compose.ui.platform.LocalLifecycleOwner.current
) {
// Your composables here
}

This mapping might be necessary on each navigation page when using Navigation Compose.

3. Manually Pass `LocalLifecycleOwner`

Manually pass `androidx.compose.ui.platform.LocalLifecycleOwner` (from Compose 1.6) to Lifecycle 2.8 methods. Here’s an example:

val state by stateFlow.collectAsStateWithLifecycle(lifecycleOwner = androidx.compose.ui.platform.LocalLifecycleOwner.current)

I tried the third solution, and it worked fine for me. Although it’s a workaround, it allows you to continue using stable versions. You can still use the Compose 1.7 Beta as suggested in the first solution. For now, I’m fine with the third solution and looking forward to the stable release of Compose 1.7.

Conclusion

The `java.lang.IllegalStateException: CompositionLocal LocalLifecycleOwner not present` error in Jetpack Compose arises due to version incompatibilities between Compose and Lifecycle components. By using the suggested workarounds, you can resolve this issue and continue developing your Compose applications smoothly. Whether you choose to update to Compose 1.7 Beta, map the `LocalLifecycleOwner`, or manually pass it, each solution has its merits depending on your project’s stability requirements and development timeline.

--

--