Function

findViewTreeComposeViewContext

Returns the ComposeViewContext used in this View's part of the hierarchy, or null if one cannot be found or it doesn't match the values set for View.findViewTreeLifecycleOwner or View.findViewTreeSavedStateRegistryOwner.

ComposeViewContextUnattachedSample

@Sampled
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)
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: