androidx.compose

Classes

Ambient

Compose passes data through the composition tree explicitly through means of parameters to composable functions.

Anchor

AndroidUiCompositionFrameClock

A CompositionFrameClock driven by an android.view.Choreographer.

AndroidUiDispatcher

A CoroutineDispatcher that will perform dispatch during choreographer's animation frame stage.

Applier

A helper class to apply changes to a tree with startNode types N given an apply adapter for type N

BitSet

BuildableMap

Composer

Implementation of a composer for mutable tree.

ComposerUpdater

CompositionCoroutineScope

A CoroutineScope used for launching side effects of a composition that also permits awaiting the next presentation frame of the composition.

CompositionReference

A CompositionReference is an opaque type that is used to logically "link" two compositions together.

EmbeddingUIContext

KeyInfo

Information about groups and their keys.

Looper

ObserverMap

A map from a key to a set of values used for keeping the relation between some entities and a models changes of which this entities are observing.

ProvidableAmbient

A ProvidableAmbient can be used in Providers to provide values.

ProvidedValue

An instance to hold a value provided by Providers and is created by the ProvidableAmbient.provides infixed operator.

Recomposer

The scheduler for performing recomposition and applying updates to one or more Compositions.

Reference

ReferenceQueue

SlotReader

SlotTable

A gap buffer implementation of the composition slot space.

SlotWriter

WeakHashMap

WeakReference

Annotations

CheckResult

Composable

Composable functions are the fundamental building blocks of an application built with Compose.

ComposableContract

This annotation can be applied to Composable functions and provide metadata to the compiler that imply compose-specific contracts that the author of the function is guaranteeing the function complies with.

ComposeCompilerApi

Direct

When applied to a composable function Direct will prevent code from being generated which allow this function's execution to be skipped or restarted.

ExperimentalComposeApi

Immutable

Immutable can be used to mark class as producing immutable instances.

InternalComposeApi

MainThread

Stable

Stable is used to communicate some guarantees to the compose compiler about how a certain type or function will behave.

StableMarker

StableMarker marks an annotation as indicating a type is stable.

TestOnly

UnionType

Untracked

When applied to a function expression Untracked will disable lambda optimizations such as tracking execution of composable function expressions or remembering a function expression value based on its capture variables.

Top-level functions summary

Unit
Observe(body: () -> Unit)

This component creates a scope which will be the root of recomposition for any reads or writes to state instances that happen inside of it.

Unit
Providers(vararg values: ProvidedValue<*>, children: () -> Unit)

Providers binds values to ProvidableAmbient keys.

Unit
Recompose(body: (recompose: () -> Unit) -> Unit)

Recompose is a component which passes a "recompose" function to its children which, when invoked, will cause its children to recompose.

ProvidableAmbient<T>
ambientOf(areEquivalent: (old: T, new: T) -> Boolean = ReferentiallyEqual, defaultFactory: () -> T = null)

Create an ambient key that can be provided using Providers.

Composition
compositionFor(container: Any, recomposer: Recomposer, parent: CompositionReference? = null, composerFactory: (SlotTable, Recomposer) -> Composer<*>)

This method is the way to initiate a composition.

Composition
compositionFor(container: Any, parent: CompositionReference? = null, composerFactory: (SlotTable, Recomposer) -> Composer<*>)

This method is the way to initiate a composition.

CompositionReference

An Effect to construct a CompositionReference at the current point of composition.

() -> Unit

Represents empty content for a Composable function.

T

Boolean

Any?

Any?

T
key(vararg inputs: Any?, block: () -> T)

key is a utility composable that is used to "group" or "key" a block of execution inside of a composition.

actual String?

Unit

Launch a suspending side effect when this composition is committed and cancel it when launchInComposition leaves the composition.

Unit
launchInComposition(v1: Any?, block: suspend CompositionCoroutineScope.() -> Unit)

Launch a suspending side effect when this composition is committed and cancel it when launchInComposition leaves the composition.

Unit
launchInComposition(v1: Any?, v2: Any?, block: suspend CompositionCoroutineScope.() -> Unit)

Launch a suspending side effect when this composition is committed and cancel it when launchInComposition leaves the composition.

Unit
launchInComposition(v1: Any?, v2: Any?, v3: Any?, block: suspend CompositionCoroutineScope.() -> Unit)

Launch a suspending side effect when this composition is committed and cancel it when launchInComposition leaves the composition.

Unit
launchInComposition(vararg keys: Any?, block: suspend CompositionCoroutineScope.() -> Unit)

Launch a suspending side effect when this composition is committed and cancel it when launchInComposition leaves the composition.

MutableState<T>
mutableStateOf(value: T, areEquivalent: (old: T, new: T) -> Boolean = ReferentiallyEqual)

Return a new MutableState initialized with the passed in value

Unit
onActive(callback: CommitScope.() -> Unit)

An effect used to observe the lifecycle of the composition.

Unit
onCommit(noinline callback: CommitScope.() -> Unit)

The onCommit effect is a lifecycle effect that will execute callback every time the composition commits.

Unit
onCommit(v1: V1, callback: CommitScope.() -> Unit)

The onCommit effect is a lifecycle effect that will execute callback every time the inputs to the effect have changed.

Unit
onCommit(v1: V1, v2: V2, callback: CommitScope.() -> Unit)

The onCommit effect is a lifecycle effect that will execute callback every time the inputs to the effect have changed.

Unit
onCommit(vararg inputs: Any?, callback: CommitScope.() -> Unit)

The onCommit effect is a lifecycle effect that will execute callback every time the inputs to the effect have changed.

Unit
onDispose(callback: () -> Unit)

An effect used to schedule work to be done when the effect leaves the composition.

Unit
onPreCommit(noinline callback: CommitScope.() -> Unit)

The onPreCommit effect is a lifecycle effect that will execute callback every time the composition commits, but before those changes have been reflected on the screen.

Unit
onPreCommit(v1: V1, callback: CommitScope.() -> Unit)

The onPreCommit effect is a lifecycle effect that will execute callback every time the inputs to the effect have changed, but before those changes have been reflected on the screen.

Unit
onPreCommit(v1: V1, v2: V2, callback: CommitScope.() -> Unit)

The onPreCommit effect is a lifecycle effect that will execute callback every time the inputs to the effect have changed, but before those changes have been reflected on the screen.

Unit
onPreCommit(vararg inputs: Any?, callback: CommitScope.() -> Unit)

The onPreCommit effect is a lifecycle effect that will execute callback every time the inputs to the effect have changed, but before those changes have been reflected on the screen.

T
remember(calculation: () -> T)

Remember the value produced by calculation.

T
remember(v1: V1, calculation: () -> T)

Remember the value returned by calculation if v1 is equal to the previous composition, otherwise produce and remember a new value by calling calculation.

T
remember(v1: V1, v2: V2, calculation: () -> T)

Remember the value returned by calculation if v1 and v2 are equal to the previous composition, otherwise produce and remember a new value by calling calculation.

T
remember(v1: V1, v2: V2, v3: V3, calculation: () -> T)

Remember the value returned by calculation if v1, v2 and v3 are equal to the previous composition, otherwise produce and remember a new value by calling calculation.

V
remember(vararg inputs: Any?, block: () -> V)

Remember the value returned by block if all values of inputs are equal to the previous composition, otherwise produce and remember a new value by calling block.

actual Unit

MutableState<T>
state(noinline areEquivalent: (old: T, new: T) -> Boolean = ReferentiallyEqual, init: () -> T)

A composable used to introduce a state value of type T into a composition.

MutableState<T>
stateFor(v1: V1, init: () -> T)

An effect to introduce a state value of type T into a composition that will last as long as the input v1 does not change.

MutableState<T>
stateFor(v1: V1, v2: V2, init: () -> T)

An effect to introduce a state value of type T into a composition that will last as long as the inputs v1 and v2 do not change.

MutableState<T>
stateFor(vararg inputs: Any?, init: () -> T)

An effect to introduce a state value of type T into a composition that will last as long as the inputs inputs do not change.

ProvidableAmbient<T>
staticAmbientOf(defaultFactory: () -> T = null)

Create an ambient key that can be provided using Providers.

suspend Unit
withRunningRecomposer(frameClock: CompositionFrameClock, block: suspend CoroutineScope.(recomposer: Recomposer) -> Unit)

Runs block with a new, active Recomposer applying changes in the calling CoroutineContext.

Top-level properties summary

(Any?, Any?) -> Boolean

Simple comparison callback that always returns false, for mutable objects that will be compared with the same reference.

(Any?, Any?) -> Boolean

Simple comparison callback using referential === equality

(Any?, Any?) -> Boolean

Simple comparison callback using structural Any.equals equality

UiComposer

Composer<*>

() -> Unit

An Effect to get the nearest invalidation lambda to the current point of composition.

Extension functions summary

For kotlin.Function0
() -> Unit
() -> Unit.orEmpty()

For kotlinx.coroutines.flow.Flow
State<T?>
Flow<T>.collectAsState(context: CoroutineContext = Dispatchers.Main)

Collects values from this Flow and represents its latest value via State.

State<R>
Flow<T>.collectAsState(initial: R, context: CoroutineContext = Dispatchers.Main)

Collects values from this Flow and represents its latest value via State.

For kotlinx.coroutines.flow.StateFlow
State<T>
StateFlow<T>.collectAsState(context: CoroutineContext = Dispatchers.Main)

Collects values from this StateFlow and represents its latest value via State.

For CompositionCoroutineScope
suspend Nothing
CompositionCoroutineScope.awaitDispose(onDispose: () -> Unit = {})

Suspends the current coroutine until the effect is disposed and the CompositionCoroutineScope is cancelled, and invokes onDispose before resuming.

For CompositionFrameClock
suspend R
CompositionFrameClock.awaitFrameMillis(crossinline onFrame: (frameTimeMillis: Long) -> R)

Suspends until a new frame is requested, immediately invokes onFrame with the frame time in nanoseconds in the calling context of frame dispatch, then resumes with the result from onFrame.

suspend Long

Suspends until a new frame is requested, returning the frame time in milliseconds.

suspend Long

Suspends until a new frame is requested, returning the frame time in nanoseconds.

suspend R
CompositionFrameClock.withFrameMillis(crossinline onFrame: (frameTimeMillis: Long) -> R)

Suspends until a new frame is requested, immediately invokes onFrame with the frame time in nanoseconds in the calling context of frame dispatch, then resumes with the result from onFrame.

For State
operator T
State<T>.getValue(thisObj: Any?, property: KProperty<*>)

Permits property delegation of vals using by for State.

For MutableState
operator Unit
MutableState<T>.setValue(thisObj: Any?, property: KProperty<*>, value: T)

Permits property delegation of vars using by for MutableState.

Top-level functions

Observe

@Composable fun Observe(body: () -> Unit): Unit

Deprecated.

This component creates a scope which will be the root of recomposition for any reads or writes to state instances that happen inside of it. This can be used to improve performance in situations where you know that a specific state object will need to change at high frequencies, and you want to reduce the burden of recomposition. It is recommended that you not introduce Observe into the composition until it is clear that is necessary to improve performance.

Parameters
body: () -> Unit The composable content to observe

Providers

@Composable fun Providers(
    vararg values: ProvidedValue<*>,
    children: () -> Unit
): Unit

Providers binds values to ProvidableAmbient keys. Reading the ambient using Ambient.current will return the value provided in Providers's values parameter for all composable functions called directly or indirectly in the children lambda.

import androidx.compose.Providers

@Composable
fun App(user: User) {
    Providers(ActiveUser provides user) {
        SomeScreen()
    }
}

Recompose

@Composable fun Recompose(body: (recompose: () -> Unit) -> Unit): Unit

Deprecated.

Recompose is a component which passes a "recompose" function to its children which, when invoked, will cause its children to recompose. This is useful if you are updating local state and need to cause a recomposition manually.

In most cases we recommend using state with immutable types in order to maintain local state inside of composables. For cases where this is impractical, Recompose can help you.

ambientOf

fun <T> ambientOf(
    areEquivalent: (old: T, new: T) -> Boolean = ReferentiallyEqual,
    defaultFactory: () -> T = null
): ProvidableAmbient<T>

Create an ambient key that can be provided using Providers. Changing the value provided during recomposition will invalidate the children of Providers that read the value using Ambient.current.

compositionFor

fun compositionFor(
    container: Any,
    recomposer: Recomposer,
    parent: CompositionReference? = null,
    composerFactory: (SlotTable, Recomposer) -> Composer<*>
): Composition

This method is the way to initiate a composition. Optionally, a parent can be provided to make the composition behave as a sub-composition of the parent. The children of container will be updated and maintained by the time this method returns.

It is important to call Composition.dispose whenever this container is no longer needed in order to release resources.

Parameters
container: Any The container whose content is being composed.
recomposer: Recomposer The Recomposer to coordinate scheduling of composition updates.
parent: CompositionReference? = null The parent composition reference, if applicable. Default is null.
composerFactory: (SlotTable, Recomposer) -> Composer<*> The factory used to created a Composer to be used by the composition.

compositionFor

fun compositionFor(
    container: Any,
    parent: CompositionReference? = null,
    composerFactory: (SlotTable, Recomposer) -> Composer<*>
): Composition

Deprecated.

This method is the way to initiate a composition. Optionally, a parent can be provided to make the composition behave as a sub-composition of the parent. The children of container will be updated and maintained by the time this method returns.

It is important to call Composition.dispose whenever this container is no longer needed in order to release resources.

Parameters
container: Any The container whose content is being composed.
parent: CompositionReference? = null The parent composition reference, if applicable. Default is null.
composerFactory: (SlotTable, Recomposer) -> Composer<*> The factory used to created a Composer to be used by the composition.

compositionReference

@Composable fun compositionReference(): CompositionReference

An Effect to construct a CompositionReference at the current point of composition. This can be used to run a separate composition in the context of the current one, preserving ambients and propagating invalidations.

emptyContent

@Stable fun emptyContent(): () -> Unit

Represents empty content for a Composable function.

See orEmpty for handling nullable Composable lambdas using empty content.

escapeCompose

inline fun <T> escapeCompose(block: NullCompilationScope.() -> T): T

isJoinedKey

fun isJoinedKey(key: Any?): Boolean

joinedKeyLeft

fun joinedKeyLeft(key: Any?): Any?

joinedKeyRight

fun joinedKeyRight(key: Any?): Any?

key

@Composable inline fun <T> key(
    vararg inputs: Any?,
    block: () -> T
): T

key is a utility composable that is used to "group" or "key" a block of execution inside of a composition. This is sometimes needed for correctness inside of control-flow that may cause a given composable invocation to execute more than once during composition.

The value for a key does not need to be globally unique, and needs only be unique amongst the invocations of key at that point in composition.

For instance, consider the following example:

import androidx.compose.key

for (user in users) {
    key(user.id) { UserPreview(user = user) }
}

for (user in users.filter { isAdmin }) {
    key(user.id) { Friend(friend = user) }
}

Even though there are users with the same id composed in both the top and the bottom loop, because they are different calls to key, there is no need to create compound keys.

The key must be unique for each element in the collection, however, or children and local state might be reused in unintended ways.

For instance, consider the following example:

import androidx.compose.key

for ((child, parent) in relationships) {
    key(parent.id) {
        User(user = child)
        User(user = parent)
    }
}
This example assumes that parent.id is a unique key for each item in the collection,but this is only true if it is fair to assume that a parent will only ever have a single child,which may not be the case. Instead, it may be more correct to do the following:
import androidx.compose.key

for ((child, parent) in relationships) {
    key(parent.id to child.id) {
        User(user = child)
        User(user = parent)
    }
}
A compound key can be created by passing in multiple arguments:
import androidx.compose.key
import androidx.compose.state

for (element in elements) {
    val selected by key(element.id, parentId) { state { false } }
    ListItem(item = element, selected = selected)
}
Parameters
vararg inputs: Any? The set of values to be used to create a compound key. These will be compared to their previous values using equals and hashCode
block: () -> T The composable children for this group.

keySourceInfoOf

actual fun keySourceInfoOf(key: Any): String?

launchInComposition

@Composable fun launchInComposition(block: suspend CompositionCoroutineScope.() -> Unit): Unit

Launch a suspending side effect when this composition is committed and cancel it when launchInComposition leaves the composition. block will run in the apply scope of the composition's Recomposer, which is usually your UI's main thread.

block will be launched once when this call enters the composition; recomposition will not cause block to launch again. To re-launch a suspend function when inputs change, see the other overloads of launchInComposition that accept input value parameters.

launchInComposition

@Composable fun launchInComposition(
    v1: Any?,
    block: suspend CompositionCoroutineScope.() -> Unit
): Unit

Launch a suspending side effect when this composition is committed and cancel it when launchInComposition leaves the composition. If v1 has changed since the last recomposition, cancel the currently running block and launch again. block will run in the apply scope of the composition's Recomposer, which is usually your UI's main thread.

launchInComposition

@Composable fun launchInComposition(
    v1: Any?,
    v2: Any?,
    block: suspend CompositionCoroutineScope.() -> Unit
): Unit

Launch a suspending side effect when this composition is committed and cancel it when launchInComposition leaves the composition. If v1 or v2 has changed since the last recomposition, cancel the currently running block and launch again. By default block will run in the apply scope of the composition's Recomposer, which is usually your UI's main thread.

launchInComposition

@Composable fun launchInComposition(
    v1: Any?,
    v2: Any?,
    v3: Any?,
    block: suspend CompositionCoroutineScope.() -> Unit
): Unit

Launch a suspending side effect when this composition is committed and cancel it when launchInComposition leaves the composition. If v1, v2 or v3 has changed since the last recomposition, cancel the currently running block and launch again. By default block will run in the apply scope of the composition's Recomposer, which is usually your UI's main thread.

launchInComposition

@Composable fun launchInComposition(
    vararg keys: Any?,
    block: suspend CompositionCoroutineScope.() -> Unit
): Unit

Launch a suspending side effect when this composition is committed and cancel it when launchInComposition leaves the composition. If keys have changed since the last recomposition, cancel the currently running block and launch again. By default block will run in the apply scope of the composition's Recomposer, which is usually your UI's main thread.

mutableStateOf

fun <T> mutableStateOf(
    value: T,
    areEquivalent: (old: T, new: T) -> Boolean = ReferentiallyEqual
): MutableState<T>

Return a new MutableState initialized with the passed in value

The MutableState class is a single value holder whose reads and writes are observed by Compose. Additionally, writes to it are transacted as part of the Framed transaction system. During composition, you will likely want to use the state and stateFor composables instead of this factory function.

Parameters
value: T the initial value for the MutableState
areEquivalent: (old: T, new: T) -> Boolean = ReferentiallyEqual a callback to compare the previous and new instance of value when it is written to. If this returns true, then no recomposition will be scheduled. See ReferentiallyEqual and StructurallyEqual for simple implementations.

onActive

@Composable fun onActive(callback: CommitScope.() -> Unit): Unit

An effect used to observe the lifecycle of the composition. The callback will execute once initially after the first composition is applied, and then will not fire again. The callback will get executed with a receiver scope that has an onDispose method which can be used to schedule a callback to be executed once whenever the effect leaves the composition

The onActive effect is essentially a convenience effect for onCommit(true) { ... }.

Parameters
callback: CommitScope.() -> Unit The lambda to execute when the composition commits for the first time and becomes active.

onCommit

@Composable inline fun onCommit(noinline callback: CommitScope.() -> Unit): Unit

The onCommit effect is a lifecycle effect that will execute callback every time the composition commits. It is useful for executing code in lock-step with composition that has side-effects. The callback will get executed with a receiver scope that has an onDispose method which can be used to schedule a callback to schedule code that cleans up the code in the callback.

Parameters
callback: CommitScope.() -> Unit The lambda to be executed when the effect is committed to the composition.

onCommit

@Composable fun <V1> onCommit(
    v1: V1,
    callback: CommitScope.() -> Unit
): Unit

The onCommit effect is a lifecycle effect that will execute callback every time the inputs to the effect have changed. It is useful for executing code in lock-step with composition that has side-effects that are based on the inputs. The callback will get executed with a receiver scope that has an onDispose method which can be used to schedule a callback to schedule code that cleans up the code in the callback.

Parameters
v1: V1 The input which will be compared across compositions to determine if callback will get executed.
callback: CommitScope.() -> Unit The lambda to be executed when the effect is committed to the composition.

onCommit

@Composable fun <V1, V2> onCommit(
    v1: V1,
    v2: V2,
    callback: CommitScope.() -> Unit
): Unit

The onCommit effect is a lifecycle effect that will execute callback every time the inputs to the effect have changed. It is useful for executing code in lock-step with composition that has side-effects that are based on the inputs. The callback will get executed with a receiver scope that has an onDispose method which can be used to schedule a callback to schedule code that cleans up the code in the callback.

Parameters
v1: V1 An input value which will be compared across compositions to determine if callback will get executed.
v2: V2 An input value which will be compared across compositions to determine if callback will get executed.
callback: CommitScope.() -> Unit The lambda to be executed when the effect is committed to the composition.

onCommit

@Composable fun onCommit(
    vararg inputs: Any?,
    callback: CommitScope.() -> Unit
): Unit

The onCommit effect is a lifecycle effect that will execute callback every time the inputs to the effect have changed. It is useful for executing code in lock-step with composition that has side-effects that are based on the inputs. The callback will get executed with a receiver scope that has an onDispose method which can be used to schedule a callback to schedule code that cleans up the code in the callback.

Parameters
vararg inputs: Any? A set of inputs which will be compared across compositions to determine if callback will get executed.
callback: CommitScope.() -> Unit The lambda to be executed when the effect is committed to the composition.

onDispose

@Composable fun onDispose(callback: () -> Unit): Unit

An effect used to schedule work to be done when the effect leaves the composition.

The onDispose effect is essentially a convenience effect for onPreCommit(true) { onDispose { ... } }.

Parameters
callback: () -> Unit The lambda to be executed when the effect leaves the composition.

onPreCommit

@Composable inline fun onPreCommit(noinline callback: CommitScope.() -> Unit): Unit

The onPreCommit effect is a lifecycle effect that will execute callback every time the composition commits, but before those changes have been reflected on the screen. It is useful for executing code that needs to update in response to a composition and it is critical that the previous results are never seen by the user. If it is not critical, onCommit is recommended instead. The callback will get executed with a receiver scope that has an onDispose method which can be used to schedule a callback to schedule code that cleans up the code in the callback.

Parameters
callback: CommitScope.() -> Unit The lambda to be executed when the effect is committed to the composition.

onPreCommit

@Composable fun <V1> onPreCommit(
    v1: V1,
    callback: CommitScope.() -> Unit
): Unit

The onPreCommit effect is a lifecycle effect that will execute callback every time the inputs to the effect have changed, but before those changes have been reflected on the screen. It is useful for executing code that needs to update in response to a composition and it is critical that the previous results are never seen by the user. If it is not critical, onCommit is recommended instead. The callback will get executed with a receiver scope that has an onDispose method which can be used to schedule a callback to schedule code that cleans up the code in the callback.

Parameters
v1: V1 The input which will be compared across compositions to determine if callback will get executed.
callback: CommitScope.() -> Unit The lambda to be executed when the effect is committed to the composition.

onPreCommit

@Composable fun <V1, V2> onPreCommit(
    v1: V1,
    v2: V2,
    callback: CommitScope.() -> Unit
): Unit

The onPreCommit effect is a lifecycle effect that will execute callback every time the inputs to the effect have changed, but before those changes have been reflected on the screen. It is useful for executing code that needs to update in response to a composition and it is critical that the previous results are never seen by the user. If it is not critical, onCommit is recommended instead. The callback will get executed with a receiver scope that has an onDispose method which can be used to schedule a callback to schedule code that cleans up the code in the callback.

Parameters
v1: V1 An input value which will be compared across compositions to determine if callback will get executed.
v2: V2 An input value which will be compared across compositions to determine if callback will get executed.
callback: CommitScope.() -> Unit The lambda to be executed when the effect is committed to the composition.

onPreCommit

@Composable fun onPreCommit(
    vararg inputs: Any?,
    callback: CommitScope.() -> Unit
): Unit

The onPreCommit effect is a lifecycle effect that will execute callback every time the inputs to the effect have changed, but before those changes have been reflected on the screen. It is useful for executing code that needs to update in response to a composition and it is critical that the previous results are never seen by the user. If it is not critical, onCommit is recommended instead. The callback will get executed with a receiver scope that has an onDispose method which can be used to schedule a callback to schedule code that cleans up the code in the callback.

Parameters
vararg inputs: Any? A set of inputs which will be compared across compositions to determine if callback will get executed.
callback: CommitScope.() -> Unit The lambda to be executed when the effect is committed to the composition.

remember

@Composable inline fun <T> remember(calculation: () -> T): T

Remember the value produced by calculation. calculation will only be evaluated during the composition. Recomposition will always return the value produced by composition.

remember

@Composable inline fun <T, V1> remember(
    v1: V1,
    calculation: () -> T
): T

Remember the value returned by calculation if v1 is equal to the previous composition, otherwise produce and remember a new value by calling calculation.

remember

@Composable inline fun <T, V1, V2> remember(
    v1: V1,
    v2: V2,
    calculation: () -> T
): T

Remember the value returned by calculation if v1 and v2 are equal to the previous composition, otherwise produce and remember a new value by calling calculation.

remember

@Composable inline fun <T, V1, V2, V3> remember(
    v1: V1,
    v2: V2,
    v3: V3,
    calculation: () -> T
): T

Remember the value returned by calculation if v1, v2 and v3 are equal to the previous composition, otherwise produce and remember a new value by calling calculation.

remember

@Composable inline fun <V> remember(
    vararg inputs: Any?,
    block: () -> V
): V

Remember the value returned by block if all values of inputs are equal to the previous composition, otherwise produce and remember a new value by calling block.

resetSourceInfo

actual fun resetSourceInfo(): Unit

state

@Composable inline fun <T> state(
    noinline areEquivalent: (old: T, new: T) -> Boolean = ReferentiallyEqual,
    init: () -> T
): MutableState<T>

A composable used to introduce a state value of type T into a composition.

This is useful when you have a value that you would like to locally mutate and use in the context of a composition. Since the returned MutableState instance implements Framed, changes to the MutableState.value property will be automatically tracked in composition and schedule a recompose.

The MutableState class can be used several different ways. For example, the most basic way is to store the returned state value into a local immutable variable, and then set the MutableState.value property on it.

import androidx.compose.state
import androidx.ui.foundation.Text
import androidx.ui.material.Button

val count = state { 0 }

Text(text = "You clicked ${count.value} times")
Button(onClick = { count.value++ }) {
    Text("Click me")
}
import androidx.compose.state
import androidx.ui.foundation.TextField
import androidx.ui.foundation.Text
import androidx.ui.input.TextFieldValue
import androidx.ui.material.Button

@Composable
fun LoginScreen() {
    var username by state { TextFieldValue("user") }
    var password by state { TextFieldValue("pass") }

    fun login() = Api.login(username.text, password.text)

    TextField(
        value = username,
        onValueChange = { username = it }
    )
    TextField(
        value = password,
        onValueChange = { password = it }
    )
    Button(onClick = { login() }) {
        Text("Login")
    }
}

In this example, LoginScreen is recomposed every time the username and password of the model updates, keeping the UI synchronized with the state.

Additionally, you can destructure the MutableState object into a value and a "setter" function.

import androidx.compose.state
import androidx.ui.foundation.Text
import androidx.ui.material.Button

val (count, setCount) = state { 0 }

Text(text = "You clicked $count times")
Button(onClick = { setCount(count + 1) }) {
    Text("Click me")
}
Finally, the MutableState instance can be used as a variable delegate to a local mutable variable.
import androidx.compose.state
import androidx.ui.foundation.Text
import androidx.ui.material.Button

var count by state { 0 }

Text(text = "You clicked $count times")
Button(onClick = { count = count + 1 }) {
    Text("Click me")
}
Parameters
areEquivalent: (old: T, new: T) -> Boolean = ReferentiallyEqual a callback to compare the previous and new instance of T when MutableState.value is written to. If this returns true, then no recomposition will be scheduled. See ReferentiallyEqual and StructurallyEqual for simple implementations.
init: () -> T A factory function to create the initial value of this state
Return
An instance of MutableState that wraps the value.

See Also

stateFor

@Composable inline fun <T, V1> stateFor(
    v1: V1,
    init: () -> T
): MutableState<T>

An effect to introduce a state value of type T into a composition that will last as long as the input v1 does not change.

This is useful when you have a value that you would like to locally mutate and use in the context of a composition, and its value is scoped to another value and you want it to be reset every time the other value changes.

The returned MutableState instance implements Framed so that changes to the MutableState.value property will be automatically tracked in composition and schedule a recompose.

import androidx.compose.onCommit
import androidx.compose.stateFor

@Composable
fun observeUser(userId: Int): User? {
    val user = stateFor<User?>(userId) { null }
    onCommit(userId) {
        val subscription = UserAPI.subscribeToUser(userId) {
            user.value = it
        }
        onDispose {
            subscription.unsubscribe()
        }
    }
    return user.value
}
Parameters
v1: V1 An input value that, when changed, will cause the state to reset and init to be rerun
init: () -> T A factory function to create the initial value of this state
Return
An instance of MutableState that wraps the value.

See Also

stateFor

@Composable inline fun <T, reified V1, reified V2> stateFor(
    v1: V1,
    v2: V2,
    init: () -> T
): MutableState<T>

An effect to introduce a state value of type T into a composition that will last as long as the inputs v1 and v2 do not change.

This is useful when you have a value that you would like to locally mutate and use in the context of a composition, and its value is scoped to another value and you want it to be reset every time the other value changes.

The returned MutableState instance implements Framed such that changes to the MutableState.value property will be automatically tracked in composition and schedule a recompose.

Parameters
v1: V1 An input value that, when changed, will cause the state to reset and init to be rerun
v2: V2 An input value that, when changed, will cause the state to reset and init to be rerun
init: () -> T A factory function to create the initial value of this state
Return
An instance of MutableState that wraps the value.

See Also

stateFor

@Composable inline fun <T> stateFor(
    vararg inputs: Any?,
    init: () -> T
): MutableState<T>

An effect to introduce a state value of type T into a composition that will last as long as the inputs inputs do not change.

This is useful when you have a value that you would like to locally mutate and use in the context of a composition, and its value is scoped to another value and you want it to be reset every time the other value changes.

The returned MutableState instance implements Framed so that changes to the MutableState.value property will be automatically tracked in composition and schedule a recompose.

Parameters
vararg inputs: Any? A set of inputs such that, when any of them have changed, will cause the state to reset and init to be rerun
init: () -> T A factory function to create the initial value of this state
Return
An instance of MutableState that wraps the value.

See Also

staticAmbientOf

fun <T> staticAmbientOf(defaultFactory: () -> T = null): ProvidableAmbient<T>

Create an ambient key that can be provided using Providers. Changing the value provided will cause the entire tree below Providers to be recomposed, disabling skipping of composable calls.

A static ambient should be only be used when the value provided is highly unlikely to change.

See Also

withRunningRecomposer

suspend fun withRunningRecomposer(
    frameClock: CompositionFrameClock,
    block: suspend CoroutineScope.(recomposer: Recomposer) -> Unit
): Unit

Runs block with a new, active Recomposer applying changes in the calling CoroutineContext. frameClock is used to align changes with display frames.

Top-level properties

NeverEqual

val NeverEqual: (Any?, Any?) -> Boolean

Simple comparison callback that always returns false, for mutable objects that will be compared with the same reference.

In this case we cannot correctly compare for equality, and so we trust that something else correctly triggered a recomposition.

ReferentiallyEqual

val ReferentiallyEqual: (Any?, Any?) -> Boolean

Simple comparison callback using referential === equality

StructurallyEqual

val StructurallyEqual: (Any?, Any?) -> Boolean

Simple comparison callback using structural Any.equals equality

composer

val composer: UiComposer

Deprecated.

currentComposer

@Composable val currentComposer: Composer<*>

invalidate

@Composable val invalidate: () -> Unit

An Effect to get the nearest invalidation lambda to the current point of composition. This can be used to trigger an invalidation on the composition locally to cause a recompose.

Extension functions

awaitDispose

suspend fun CompositionCoroutineScope.awaitDispose(onDispose: () -> Unit = {}): Nothing

Suspends the current coroutine until the effect is disposed and the CompositionCoroutineScope is cancelled, and invokes onDispose before resuming. awaitDispose never resumes normally and will always throw either kotlinx.coroutines.CancellationException or the exception that failed the current kotlinx.coroutines.Job.

awaitFrameMillis

inline suspend fun <R> CompositionFrameClock.awaitFrameMillis(crossinline onFrame: (frameTimeMillis: Long) -> R): R

Deprecated.

Suspends until a new frame is requested, immediately invokes onFrame with the frame time in nanoseconds in the calling context of frame dispatch, then resumes with the result from onFrame.

frameTimeNanos should be used when calculating animation time deltas from frame to frame as it may be normalized to the target time for the frame, not necessarily a direct, "now" value.

The time base of the value provided by awaitFrameNanos is implementation defined. Time values provided are monotonically increasing; after a call to awaitFrameNanos completes it must not provide the same value again for a subsequent call.

awaitFrameMillis

suspend fun CompositionFrameClock.awaitFrameMillis(): Long

Deprecated.

Suspends until a new frame is requested, returning the frame time in milliseconds. This value should be used when calculating animation time deltas from frame to frame as it may be normalized to the target time for the frame, not necessarily a direct, "now" value.

The time base of the value returned by awaitFrameMillis is implementation defined. Time values returned are monotonically increasing; after a call to awaitFrameMillis returns it must not return the same value again for a subsequent call.

awaitFrameNanos

suspend fun CompositionFrameClock.awaitFrameNanos(): Long

Deprecated.

Suspends until a new frame is requested, returning the frame time in nanoseconds. This value should be used when calculating animation time deltas from frame to frame as it may be normalized to the target time for the frame, not necessarily a direct, "now" value.

The time base of the value returned by awaitFrameNanos is implementation defined. Time values returned are monotonically increasing; after a call to awaitFrameNanos returns it must not return the same value again for a subsequent call.

collectAsState

@Composable inline fun <T> Flow<T>.collectAsState(context: CoroutineContext = Dispatchers.Main): State<T?>

Deprecated.

Collects values from this Flow and represents its latest value via State. Every time there would be new value posted into the Flow the returned State will be updated causing recomposition of every State.value usage.

Parameters
context: CoroutineContext = Dispatchers.Main CoroutineContext to use for collecting.

collectAsState

@Composable fun <T : R, R> Flow<T>.collectAsState(
    initial: R,
    context: CoroutineContext = Dispatchers.Main
): State<R>

Collects values from this Flow and represents its latest value via State. Every time there would be new value posted into the Flow the returned State will be updated causing recomposition of every State.value usage.

import androidx.compose.collectAsState
import androidx.ui.foundation.Text

val value: String by flow.collectAsState("initial")
Text("Value is $value")
Parameters
context: CoroutineContext = Dispatchers.Main CoroutineContext to use for collecting.

collectAsState

@ExperimentalCoroutinesApi @Composable inline fun <T> StateFlow<T>.collectAsState(context: CoroutineContext = Dispatchers.Main): State<T>

Collects values from this StateFlow and represents its latest value via State. The StateFlow.value is used as an initial value. Every time there would be new value posted into the StateFlow the returned State will be updated causing recomposition of every State.value usage.

import androidx.compose.collectAsState
import androidx.ui.foundation.Text

val value: String by stateFlow.collectAsState()
Text("Value is $value")
Parameters
context: CoroutineContext = Dispatchers.Main CoroutineContext to use for collecting.

getValue

inline operator fun <T> State<T>.getValue(
    thisObj: Any?,
    property: KProperty<*>
): T

Permits property delegation of vals using by for State.

import androidx.ui.foundation.Text
import androidx.ui.layout.Row

// Composable function that manages a subscription to a data source, returning it as State
@Composable
fun observeSampleData(): State<String> = TODO()

// Subscription is managed here, but currentValue is not read yet
val currentValue by observeSampleData()

Row {
    // This scope will recompose when currentValue changes
    Text("Data: $currentValue")
}

orEmpty

inline fun (() -> Unit).orEmpty(): () -> Unit
Return
this Composable if not null, else emptyContent.

setValue

inline operator fun <T> MutableState<T>.setValue(
    thisObj: Any?,
    property: KProperty<*>,
    value: T
): Unit

Permits property delegation of vars using by for MutableState.

import androidx.compose.state
import androidx.ui.foundation.Text
import androidx.ui.material.Button

var count by state { 0 }

Text(text = "You clicked $count times")
Button(onClick = { count = count + 1 }) {
    Text("Click me")
}

withFrameMillis

inline suspend fun <R> CompositionFrameClock.withFrameMillis(crossinline onFrame: (frameTimeMillis: Long) -> R): R

Suspends until a new frame is requested, immediately invokes onFrame with the frame time in nanoseconds in the calling context of frame dispatch, then resumes with the result from onFrame.

frameTimeNanos should be used when calculating animation time deltas from frame to frame as it may be normalized to the target time for the frame, not necessarily a direct, "now" value.

The time base of the value provided by CompositionFrameClock.withFrameMillis is implementation defined. Time values provided are monotonically increasing; after a call to CompositionFrameClock.withFrameMillis completes it must not provide the same value again for a subsequent call.