Stay organized with collections
Save and categorize content based on your preferences.
Extension functions summary
Extension functions
@Composable
fun <T : Any?> LiveData<T>.observeAsState(): State<T?>
Starts observing this LiveData
and represents its values via State
. Every time there would be new value posted into the LiveData
the returned State
will be updated causing recomposition of every State.value
usage.
The inner observer will automatically be removed when this composable disposes or the current LifecycleOwner
moves to the Lifecycle.State.DESTROYED
state.
import androidx.compose.material.Text
import androidx.compose.runtime.livedata.observeAsState
val value: String? by liveData.observeAsState()
Text("Value is $value")
@Composable
fun <R : Any?, T : R> LiveData<T>.observeAsState(initial: R): State<R>
Starts observing this LiveData
and represents its values via State
. Every time there would be new value posted into the LiveData
the returned State
will be updated causing recomposition of every State.value
usage.
The initial
value will be used only if this LiveData is not already initialized
. Note that if T
is a non-null type, it is your responsibility to ensure that any value you set on this LiveData is also non-null.
The inner observer will automatically be removed when this composable disposes or the current LifecycleOwner
moves to the Lifecycle.State.DESTROYED
state.
import androidx.compose.material.Text
import androidx.compose.runtime.livedata.observeAsState
val value: String by liveData.observeAsState("initial")
Text("Value is $value")
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-05-15 UTC.
[null,null,["Last updated 2025-05-15 UTC."],[],[],null,["# androidx.compose.runtime.livedata\n=================================\n\nExtension functions summary\n---------------------------\n\n|-----------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [State](/reference/kotlin/androidx/compose/runtime/State)`\u003cT?\u003e` | `@`[Composable](/reference/kotlin/androidx/compose/runtime/Composable) `\u003cT : `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`?\u003e `[LiveData](/reference/kotlin/androidx/lifecycle/LiveData)`\u003cT\u003e.`[observeAsState](/reference/kotlin/androidx/compose/runtime/livedata/package-summary#(androidx.lifecycle.LiveData).observeAsState())`()` Starts observing this [LiveData](/reference/kotlin/androidx/lifecycle/LiveData) and represents its values via [State](/reference/kotlin/androidx/compose/runtime/State). |\n| [State](/reference/kotlin/androidx/compose/runtime/State)`\u003cR\u003e` | `@`[Composable](/reference/kotlin/androidx/compose/runtime/Composable) `\u003cR : `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`?, T : R\u003e `[LiveData](/reference/kotlin/androidx/lifecycle/LiveData)`\u003cT\u003e.`[observeAsState](/reference/kotlin/androidx/compose/runtime/livedata/package-summary#(androidx.lifecycle.LiveData).observeAsState(kotlin.Any))`(initial: R)` Starts observing this [LiveData](/reference/kotlin/androidx/lifecycle/LiveData) and represents its values via [State](/reference/kotlin/androidx/compose/runtime/State). |\n\nExtension functions\n-------------------\n\n### observeAsState\n\nArtifact: [androidx.compose.runtime:runtime-livedata](/jetpack/androidx/releases/compose-runtime) \n[View Source](https://cs.android.com/search?q=file:androidx/compose/runtime/livedata/LiveDataAdapter.kt+function:observeAsState) \nAdded in [1.0.0](/jetpack/androidx/releases/compose-runtime#1.0.0) \n\n```\n@Composable\nfun \u003cT : Any?\u003e LiveData\u003cT\u003e.observeAsState(): State\u003cT?\u003e\n```\n\nStarts observing this [LiveData](/reference/kotlin/androidx/lifecycle/LiveData) and represents its values via [State](/reference/kotlin/androidx/compose/runtime/State). Every time there would be new value posted into the [LiveData](/reference/kotlin/androidx/lifecycle/LiveData) the returned [State](/reference/kotlin/androidx/compose/runtime/State) will be updated causing recomposition of every [State.value](/reference/kotlin/androidx/compose/runtime/State#value()) usage.\n\nThe inner observer will automatically be removed when this composable disposes or the current [LifecycleOwner](/reference/kotlin/androidx/lifecycle/LifecycleOwner) moves to the [Lifecycle.State.DESTROYED](/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED) state. \n\n```kotlin\nimport androidx.compose.material.Text\nimport androidx.compose.runtime.livedata.observeAsState\n\nval value: String? by liveData.observeAsState()\nText(\"Value is $value\")\n``` \n\n### observeAsState\n\nArtifact: [androidx.compose.runtime:runtime-livedata](/jetpack/androidx/releases/compose-runtime) \n[View Source](https://cs.android.com/search?q=file:androidx/compose/runtime/livedata/LiveDataAdapter.kt+function:observeAsState) \nAdded in [1.0.0](/jetpack/androidx/releases/compose-runtime#1.0.0) \n\n```\n@Composable\nfun \u003cR : Any?, T : R\u003e LiveData\u003cT\u003e.observeAsState(initial: R): State\u003cR\u003e\n```\n\nStarts observing this [LiveData](/reference/kotlin/androidx/lifecycle/LiveData) and represents its values via [State](/reference/kotlin/androidx/compose/runtime/State). Every time there would be new value posted into the [LiveData](/reference/kotlin/androidx/lifecycle/LiveData) the returned [State](/reference/kotlin/androidx/compose/runtime/State) will be updated causing recomposition of every [State.value](/reference/kotlin/androidx/compose/runtime/State#value()) usage.\n\nThe [initial](/reference/kotlin/androidx/compose/runtime/livedata/package-summary#(androidx.lifecycle.LiveData).observeAsState(kotlin.Any)) value will be used only if this LiveData is not already [initialized](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/index.html). Note that if [T](/reference/kotlin/androidx/compose/runtime/livedata/package-summary#(androidx.lifecycle.LiveData).observeAsState(kotlin.Any)) is a non-null type, it is your responsibility to ensure that any value you set on this LiveData is also non-null.\n\nThe inner observer will automatically be removed when this composable disposes or the current [LifecycleOwner](/reference/kotlin/androidx/lifecycle/LifecycleOwner) moves to the [Lifecycle.State.DESTROYED](/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED) state. \n\n```kotlin\nimport androidx.compose.material.Text\nimport androidx.compose.runtime.livedata.observeAsState\n\nval value: String by liveData.observeAsState(\"initial\")\nText(\"Value is $value\")\n```"]]