Compose Unstyled 2.0 is out! Check the official announcement blog ->
Class

ComposeViewContext

ComposeViewContext can be used to compose a ComposeView while it isn't attached to the view hierarchy.

Source set: Android
class ComposeViewContext
private constructor(
    composeViewContext: ComposeViewContext?,
    internal val view: View,
    compositionContext: CompositionContext?,
    lifecycleOwner: LifecycleOwner?,
    savedStateRegistryOwner: SavedStateRegistryOwner?,
    viewModelStoreOwner: ViewModelStoreOwner?,
    matchesContext: Boolean = composeViewContext?.view?.context == view.context,
)

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.

Secondary Constructors

constructor(
    view: View,
    compositionContext: CompositionContext? = null,
    lifecycleOwner: LifecycleOwner? = null,
    savedStateRegistryOwner: SavedStateRegistryOwner? = null,
    viewModelStoreOwner: ViewModelStoreOwner? = null,
) : this(
    view.findViewTreeComposeViewContext(),
    view,
    compositionContext,
    lifecycleOwner,
    savedStateRegistryOwner,
    viewModelStoreOwner,
)

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

Parameters

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 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 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 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 to be used by ComposeViews to create RetainedValuesStores. If null, the default value is obtained from View.findViewTreeViewModelStoreOwner.

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 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 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 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 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 to be used by ComposeViews to create RetainedValuesStores. If null, the default value is obtained from View.findViewTreeViewModelStoreOwner.

Last updated: