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 = view.findViewTreeCompositionContext() ?: view.windowRecomposer,
    lifecycleOwner: LifecycleOwner = view.findViewTreeLifecycleOwner() ?: throw IllegalStateException( "Composed into a View which doesn't propagate ViewTreeLifecycleOwner!" ),
    savedStateRegistryOwner: SavedStateRegistryOwner = view.findViewTreeSavedStateRegistryOwner() ?: throw IllegalStateException( "Composed into a View which doesn't propagate ViewTreeSavedStateRegistryOwner!" ),
    viewModelStoreOwner: ViewModelStoreOwner? = view.findViewTreeViewModelStoreOwner()
)

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 calling this constructor and should be attached as long as ComposeViewContext is expected to be around is expected to be around.

compositionContext: CompositionContext = view.findViewTreeCompositionContext() ?: view.windowRecomposer

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

lifecycleOwner: LifecycleOwner = view.findViewTreeLifecycleOwner() ?: throw IllegalStateException( "Composed into a View which doesn't propagate ViewTreeLifecycleOwner!" )

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

savedStateRegistryOwner: SavedStateRegistryOwner = view.findViewTreeSavedStateRegistryOwner() ?: throw IllegalStateException( "Composed into a View which doesn't propagate ViewTreeSavedStateRegistryOwner!" )

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

viewModelStoreOwner: ViewModelStoreOwner? = view.findViewTreeViewModelStoreOwner()

ViewModelStoreOwner to be used by ComposeViews to create RetainedValuesStores. 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 calling this constructor.

compositionContext: CompositionContext = this.compositionContext

The CompositionContext used by ComposeViews constructed with this ComposeViewContext.

lifecycleOwner: LifecycleOwner = this.lifecycleOwner

Used to govern the lifecycle-important aspects of ComposeViews constructed with this ComposeViewContext.

savedStateRegistryOwner: SavedStateRegistryOwner = this.savedStateRegistryOwner

The SavedStateRegistryOwner used by ComposeViews constructed with this ComposeViewContext.

viewModelStoreOwner: ViewModelStoreOwner? = this.viewModelStoreOwner

ViewModelStoreOwner to be used by ComposeViews to create RetainedValuesStores.