@ExperimentalComposeViewContextApi
class ComposeViewContext
private constructor(
composeViewContext: ComposeViewContext?,
internal val view: View,
internal val compositionContext: CompositionContext,
internal val lifecycleOwner: LifecycleOwner,
internal val savedStateRegistryOwner: SavedStateRegistryOwner,
internal val 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 =
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(),
) : 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 calling this constructor and should be attached as long as ComposeViewContext is expected to be around is expected to be around. |
| compositionContext | 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 | 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 | 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 to be used by ComposeViews to create RetainedValuesStores. The default value is obtained from View.findViewTreeViewModelStoreOwner. |
Functions
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 calling this constructor. |
| compositionContext | The CompositionContext used by ComposeViews constructed with this ComposeViewContext. |
| lifecycleOwner | Used to govern the lifecycle-important aspects of ComposeViews constructed with this ComposeViewContext. |
| savedStateRegistryOwner | The SavedStateRegistryOwner used by ComposeViews constructed with this ComposeViewContext. |
| viewModelStoreOwner | ViewModelStoreOwner to be used by ComposeViews to create RetainedValuesStores. |
Code Examples
ComposeViewContextUnattachedSample
@OptIn(ExperimentalComposeViewContextApi::class)
fun ComposeViewContextUnattachedSample(attachedView: View) {
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)
}