# ComposeViewContext

Artifact: [androidx.compose.ui:ui](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/platform/ComposeViewContext.android.kt+class:androidx.compose.ui.platform.ComposeViewContext) Added in [1.11.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.11.0) Common/AllAndroid/JVM
- android

  ```
  class ComposeViewContext
  ```

*** ** * ** ***

`https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext` can be used to compose a `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView` while it isn't attached to the view hierarchy. This is useful when prefetching items for a RecyclerView, for example. To use it, call `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/AbstractComposeView#createComposition()` with the `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext` after the content has been set. If the `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView` is never attached to the hierarchy, `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/AbstractComposeView#disposeComposition()` must be called to release resources and stop composition. If the `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView` is attached to the hierarchy, it will stop composition once it has been removed from the hierarchy and calling `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/AbstractComposeView#disposeComposition()` is unnecessary. It will start again if the `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView#setContent(kotlin.Function0)` is called again, the View is reattached to the hierarchy, or `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/AbstractComposeView#createComposition()` is called again.

```kotlin
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ComposeViewContext
import androidx.compose.ui.platform.findViewTreeComposeViewContext

val composeView = ComposeView(attachedView.context)
composeView.setContent { Box(Modifier.fillMaxSize()) }
// If a ComposeViewContext hasn't been attached to the hierarchy, create a new one for this view
val composeViewContext =
    attachedView.findViewTreeComposeViewContext() ?: ComposeViewContext(attachedView)
// start composing while composeView isn't attached
composeView.createComposition(composeViewContext)
```

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext#ComposeViewContext(android.view.View,androidx.compose.runtime.CompositionContext,androidx.lifecycle.LifecycleOwner,androidx.savedstate.SavedStateRegistryOwner,androidx.lifecycle.ViewModelStoreOwner)( view: https://developer.android.com/reference/android/view/View.html, compositionContext: https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionContext?, lifecycleOwner: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner?, savedStateRegistryOwner: https://developer.android.com/reference/kotlin/androidx/savedstate/SavedStateRegistryOwner?, viewModelStoreOwner: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner? )` Constructs a `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext` to be used with `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/AbstractComposeView#createComposition()` to compose content while the `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/AbstractComposeView` isn't attached. | android |

| ### Public functions |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext#copy(android.view.View,androidx.compose.runtime.CompositionContext,androidx.lifecycle.LifecycleOwner,androidx.savedstate.SavedStateRegistryOwner,androidx.lifecycle.ViewModelStoreOwner)( view: https://developer.android.com/reference/android/view/View.html, compositionContext: https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionContext?, lifecycleOwner: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner?, savedStateRegistryOwner: https://developer.android.com/reference/kotlin/androidx/savedstate/SavedStateRegistryOwner?, viewModelStoreOwner: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner? )` Construct a `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext` sharing parts with another `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext`. | android |

## Public constructors

### ComposeViewContext

android Added in [1.11.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.11.0)

```
ComposeViewContext(
    view: View,
    compositionContext: CompositionContext? = null,
    lifecycleOwner: LifecycleOwner? = null,
    savedStateRegistryOwner: SavedStateRegistryOwner? = null,
    viewModelStoreOwner: ViewModelStoreOwner? = null
)
```

Constructs a `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext` to be used with `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/AbstractComposeView#createComposition()` to compose content while the `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/AbstractComposeView` isn't attached.

| Parameters |
|---|---|
| `view: https://developer.android.com/reference/android/view/View.html` | A `https://developer.android.com/reference/android/view/View.html` attached to the same hierarchy as the `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView`s constructed with this `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext`. This `https://developer.android.com/reference/android/view/View.html` must be attached before a `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView` using the `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext` has called `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView#setContent(kotlin.Function0)`. `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext#(android.view.View,androidx.compose.runtime.CompositionContext,androidx.lifecycle.LifecycleOwner,androidx.savedstate.SavedStateRegistryOwner,androidx.lifecycle.ViewModelStoreOwner)` must remain attached as long as the `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext` is expected to be active. |
| `compositionContext: https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionContext? = null` | The `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionContext` used by `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView`s constructed with this `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext`. If `null`, the default value is used, obtained from `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/package-summary#(android.view.View).findViewTreeCompositionContext()`, or, if not found from the window `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Recomposer`. |
| `lifecycleOwner: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner? = null` | Used to govern the lifecycle-important aspects of `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView`s constructed with this `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext`. If `null`, the default value is obtained from `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(android.view.View).findViewTreeLifecycleOwner()`. If not found, `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` will be thrown during composition. |
| `savedStateRegistryOwner: https://developer.android.com/reference/kotlin/androidx/savedstate/SavedStateRegistryOwner? = null` | The `https://developer.android.com/reference/kotlin/androidx/savedstate/SavedStateRegistryOwner` used by `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView`s constructed with this `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext`. If `null`, the default value is obtained from `https://developer.android.com/reference/kotlin/androidx/savedstate/package-summary#(android.view.View).findViewTreeSavedStateRegistryOwner()`. If not found, an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` will be thrown during composition. |
| `viewModelStoreOwner: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner? = null` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner` to be used by `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView`s to create `https://developer.android.com/reference/kotlin/androidx/compose/runtime/retain/RetainedValuesStore`s. If `null`, the default value is obtained from `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(android.view.View).findViewTreeViewModelStoreOwner()`. |

## Public functions

### copy

android Added in [1.11.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.11.0)

```
fun copy(
    view: View = this.view,
    compositionContext: CompositionContext? = this._compositionContext,
    lifecycleOwner: LifecycleOwner? = this._lifecycleOwner,
    savedStateRegistryOwner: SavedStateRegistryOwner? = this._savedStateRegistryOwner,
    viewModelStoreOwner: ViewModelStoreOwner? = this._viewModelStoreOwner
): ComposeViewContext
```

Construct a `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext` sharing parts with another `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext`.

| Parameters |
|---|---|
| `view: https://developer.android.com/reference/android/view/View.html = this.view` | A `https://developer.android.com/reference/android/view/View.html` attached to the same hierarchy as the `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView`s constructed with this `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext`. This `https://developer.android.com/reference/android/view/View.html` must be attached before a `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView` using the `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext` has called `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView#setContent(kotlin.Function0)`. `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext#copy(android.view.View,androidx.compose.runtime.CompositionContext,androidx.lifecycle.LifecycleOwner,androidx.savedstate.SavedStateRegistryOwner,androidx.lifecycle.ViewModelStoreOwner)` must remain attached as long as the `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext` is expected to be active. |
| `compositionContext: https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionContext? = this._compositionContext` | The `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionContext` used by `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView`s constructed with this `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext`. If `null`, the default value is used, obtained from `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/package-summary#(android.view.View).findViewTreeCompositionContext()`, or, if not found from the window `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Recomposer`. |
| `lifecycleOwner: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner? = this._lifecycleOwner` | Used to govern the lifecycle-important aspects of `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView`s constructed with this `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext`. If `null`, the default value is obtained from `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(android.view.View).findViewTreeLifecycleOwner()`. If not found, `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` will be thrown during composition. |
| `savedStateRegistryOwner: https://developer.android.com/reference/kotlin/androidx/savedstate/SavedStateRegistryOwner? = this._savedStateRegistryOwner` | The `https://developer.android.com/reference/kotlin/androidx/savedstate/SavedStateRegistryOwner` used by `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView`s constructed with this `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeViewContext`. If `null`, the default value is obtained from `https://developer.android.com/reference/kotlin/androidx/savedstate/package-summary#(android.view.View).findViewTreeSavedStateRegistryOwner()`. If not found, an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` will be thrown during composition. |
| `viewModelStoreOwner: https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner? = this._viewModelStoreOwner` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner` to be used by `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/ComposeView`s to create `https://developer.android.com/reference/kotlin/androidx/compose/runtime/retain/RetainedValuesStore`s. If `null`, the default value is obtained from `https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(android.view.View).findViewTreeViewModelStoreOwner()`. |