ComposeViewContext


ComposeViewContext can be used to compose a 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 AbstractComposeView.createComposition with the ComposeViewContext after the content has been set. If the ComposeView is never attached to the hierarchy, AbstractComposeView.disposeComposition must be called to release resources and stop composition. If the ComposeView is attached to the hierarchy, it will stop composition once it has been removed from the hierarchy and calling AbstractComposeView.disposeComposition is unnecessary. It will start again if the ComposeView.setContent is called again, the View is reattached to the hierarchy, or AbstractComposeView.createComposition is called again.

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

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

Constructs a ComposeViewContext to be used with AbstractComposeView.createComposition to compose content while the AbstractComposeView isn't attached.

android

Public functions

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

Construct a ComposeViewContext sharing parts with another ComposeViewContext.

android

Public constructors

ComposeViewContext

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

Constructs a ComposeViewContext to be used with AbstractComposeView.createComposition to compose content while the AbstractComposeView isn't attached.

Parameters
view: View

A View attached to the same hierarchy as the ComposeViews constructed with this ComposeViewContext. This View must be attached before a ComposeView using the ComposeViewContext has called ComposeView.setContent. view must remain attached as long as the ComposeViewContext is expected to be active.

compositionContext: CompositionContext? = null

The CompositionContext used by ComposeViews constructed with this ComposeViewContext. If null, the default value is used, obtained from View.findViewTreeCompositionContext, or, if not found from the window androidx.compose.runtime.Recomposer.

lifecycleOwner: LifecycleOwner? = null

Used to govern the lifecycle-important aspects of ComposeViews constructed with this ComposeViewContext. If null, the default value is obtained from View.findViewTreeLifecycleOwner. If not found, IllegalStateException will be thrown during composition.

savedStateRegistryOwner: SavedStateRegistryOwner? = null

The SavedStateRegistryOwner used by ComposeViews constructed with this ComposeViewContext. If null, the default value is obtained from View.findViewTreeSavedStateRegistryOwner. If not found, an IllegalStateException will be thrown during composition.

viewModelStoreOwner: ViewModelStoreOwner? = null

ViewModelStoreOwner to be used by ComposeViews to create RetainedValuesStores. If null, the default value is obtained from View.findViewTreeViewModelStoreOwner.

Public functions

copy

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 ComposeViewContext sharing parts with another ComposeViewContext.

Parameters
view: View = this.view

A View attached to the same hierarchy as the ComposeViews constructed with this ComposeViewContext. This View must be attached before a ComposeView using the ComposeViewContext has called ComposeView.setContent. view must remain attached as long as the ComposeViewContext is expected to be active.

compositionContext: CompositionContext? = this._compositionContext

The CompositionContext used by ComposeViews constructed with this ComposeViewContext. If null, the default value is used, obtained from View.findViewTreeCompositionContext, or, if not found from the window androidx.compose.runtime.Recomposer.

lifecycleOwner: LifecycleOwner? = this._lifecycleOwner

Used to govern the lifecycle-important aspects of ComposeViews constructed with this ComposeViewContext. If null, the default value is obtained from View.findViewTreeLifecycleOwner. If not found, IllegalStateException will be thrown during composition.

savedStateRegistryOwner: SavedStateRegistryOwner? = this._savedStateRegistryOwner

The SavedStateRegistryOwner used by ComposeViews constructed with this ComposeViewContext. If null, the default value is obtained from View.findViewTreeSavedStateRegistryOwner. If not found, an IllegalStateException will be thrown during composition.

viewModelStoreOwner: ViewModelStoreOwner? = this._viewModelStoreOwner

ViewModelStoreOwner to be used by ComposeViews to create RetainedValuesStores. If null, the default value is obtained from View.findViewTreeViewModelStoreOwner.