# Composition

Artifact: [androidx.compose.runtime:runtime](https://developer.android.com/jetpack/androidx/releases/compose-runtime) [View Source](https://cs.android.com/search?q=file:androidx/compose/runtime/Composition.kt+class:androidx.compose.runtime.Composition) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0) Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  interface Composition
  ```
Known direct subclasses [ControlledComposition](https://developer.android.com/reference/kotlin/androidx/compose/runtime/ControlledComposition), [ReusableComposition](https://developer.android.com/reference/kotlin/androidx/compose/runtime/ReusableComposition)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/ControlledComposition` | A controlled composition is a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition` that can be directly controlled by the caller. |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/ReusableComposition` | A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/ReusableComposition` is a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition` that can be reused for different composable content. |

Known indirect subclasses [PausableComposition](https://developer.android.com/reference/kotlin/androidx/compose/runtime/PausableComposition)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/PausableComposition` | A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/PausableComposition` is a sub-composition that can be composed incrementally as it supports being paused and resumed. |

*** ** * ** ***

A composition object is usually constructed for you, and returned from an API that is used to initially compose a UI. For instance, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition#setContent(kotlin.Function0)` returns a Composition.

The `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition#dispose()` method should be used when you would like to dispose of the UI and the Composition.

## Summary

| ### Public functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition#dispose()()` Clear the hierarchy that was created from the composition and release resources allocated for composition. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition#setContent(kotlin.Function0)(content: @https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` Update the composition with the content described by the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition#setContent(kotlin.Function0)` composable. | Cmn |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition#hasInvalidations()` Returns true if any pending invalidations have been scheduled. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition#isDisposed()` True if `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition#dispose()` has been called. | Cmn |

| ### Extension functions |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/tooling/CompositionObserverHandle?` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/ExperimentalComposeRuntimeApi https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition.https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition#(androidx.compose.runtime.Composition).setObserver(androidx.compose.runtime.tooling.CompositionObserver)(observer: https://developer.android.com/reference/kotlin/androidx/compose/runtime/tooling/CompositionObserver)` Observe the composition. | Cmn |

## Public functions

### dispose

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
fun dispose(): Unit
```

Clear the hierarchy that was created from the composition and release resources allocated for composition. After calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition#dispose()` the composition will no longer be recomposed and calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition#setContent(kotlin.Function0)` will throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html`. Calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition#dispose()` is idempotent, all calls after the first are a no-op.

### setContent

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
fun setContent(content: @Composable () -> Unit): Unit
```

Update the composition with the content described by the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition#setContent(kotlin.Function0)` composable. After this has been called the changes to produce the initial composition has been calculated and applied to the composition.

Will throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` if the composition has been disposed.

| Parameters |
|---|---|
| `content: @https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | A composable function that describes the content of the composition. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | thrown in the composition has been `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition#dispose()`d. |

## Public properties

### hasInvalidations

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
val hasInvalidations: Boolean
```

Returns true if any pending invalidations have been scheduled. An invalidation is schedule if `https://developer.android.com/reference/kotlin/androidx/compose/runtime/RecomposeScope#invalidate()` has been called on any composition scopes create for the composition.

Modifying `https://developer.android.com/reference/kotlin/androidx/compose/runtime/MutableState#value()` of a value produced by `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#mutableStateOf(kotlin.Any,androidx.compose.runtime.SnapshotMutationPolicy)` will automatically call `https://developer.android.com/reference/kotlin/androidx/compose/runtime/RecomposeScope#invalidate()` for any scope that read `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State#value()` of the mutable state instance during composition.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/RecomposeScope` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#mutableStateOf(kotlin.Any,androidx.compose.runtime.SnapshotMutationPolicy)` |   |

### isDisposed

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
val isDisposed: Boolean
```

True if `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composition#dispose()` has been called.

## Extension functions

### Composition.setObserver

Cmn Artifact: [androidx.compose.runtime:runtime](https://developer.android.com/jetpack/androidx/releases/compose-runtime) [View Source](https://cs.android.com/search?q=file:androidx/compose/runtime/tooling/CompositionObserver.kt+function:setObserver)

```
@ExperimentalComposeRuntimeApi
fun Composition.setObserver(observer: CompositionObserver): CompositionObserverHandle?
```

Observe the composition. Calling this twice on the same composition will implicitly dispose the previous observer. the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/tooling/CompositionObserver` will be called for this composition and all sub-composition, transitively, for which this composition is a context. If `https://developer.android.com/reference/kotlin/androidx/compose/runtime/tooling/package-summary#(androidx.compose.runtime.Composition).setObserver(androidx.compose.runtime.tooling.CompositionObserver)` is called on a sub-composition, it will override the parent composition observer for itself and all its sub-compositions.

The observer is resolved once per composition pass, between `https://developer.android.com/reference/kotlin/androidx/compose/runtime/tooling/CompositionObserver#onBeginComposition(androidx.compose.runtime.tooling.ObservableComposition)` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/tooling/CompositionObserver#onEndComposition(androidx.compose.runtime.tooling.ObservableComposition)`. Setting or replacing the observer while a pass is in progress takes effect starting with the next pass.

| Parameters |
|---|---|
| `observer: https://developer.android.com/reference/kotlin/androidx/compose/runtime/tooling/CompositionObserver` | the observer that will be informed of composition events for this composition and all sub-compositions for which this composition is the composition context. Observing a composition will prevent the parent composition's observer from receiving composition events about this composition. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/tooling/CompositionObserverHandle?` | a handle that allows the observer to be disposed and detached from the composition. Disposing an observer for a composition with a parent observer will begin sending the events to the parent composition's observer. A `null` indicates the composition does not support being observed. |