AbstractComposeView

Class
Android
abstract class AbstractComposeView
@JvmOverloads
constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    ViewGroup(context, attrs, defStyleAttr)

Base class for custom android.view.Views implemented using Jetpack Compose UI. Subclasses should implement the Content function with the appropriate content. Calls to addView and its variants and overloads will fail with IllegalStateException.

By default, the composition is disposed according to ViewCompositionStrategy.Default. Call disposeComposition to dispose of the underlying composition earlier, or if the view is never initially attached to a window. (The requirement to dispose of the composition explicitly in the event that the view is never (re)attached is temporary.)

AbstractComposeView only supports being added into view hierarchies propagating LifecycleOwner and SavedStateRegistryOwner via androidx.lifecycle.setViewTreeLifecycleOwner and androidx.savedstate.setViewTreeSavedStateRegistryOwner. In most cases you will already have it set up correctly as androidx.activity.ComponentActivity, androidx.fragment.app.Fragment and androidx.navigation.NavController will provide the correct values.

Properties

Android
@InternalComposeUiApi


var showLayoutBounds: Boolean

Enables the display of visual layout bounds for the Compose UI content of this view. This is typically configured using the system developer setting for "Show layout bounds."

Android
var autoClearFocusBehavior: AutoClearFocusBehavior

Controls behavior for how focus should be automatically cleared for this ComposeView when responding to input. The default value is AutoClearFocusBehavior.Default.

This property should be set prior to first composition.

Android
val hasComposition: Boolean

true if this View is host to an active Compose UI composition. An active composition may consume resources.

Functions

fun setParentCompositionContext(parent: CompositionContext?)

Set the CompositionContext that should be the parent of this view's composition. If parent is null it will be determined automatically from the window the view is attached to.

fun setViewCompositionStrategy(strategy: ViewCompositionStrategy)

Set the strategy for managing disposal of this View's internal composition. Defaults to ViewCompositionStrategy.Default.

This View's composition is a live resource that must be disposed to ensure that long-lived references to it do not persist

See ViewCompositionStrategy for more information.

@Composable  abstract fun Content()

The Jetpack Compose UI content for this view. Subclasses must implement this method to provide content. Initial composition will occur when the view becomes attached to a window or when createComposition is called, whichever comes first.

fun createComposition()

Perform initial composition for this view. Once this method is called or the view becomes attached to a window, either disposeComposition must be called or the LifecycleOwner returned by findViewTreeLifecycleOwner must reach the Lifecycle.State.DESTROYED state for the composition to be cleaned up properly. (This restriction is temporary.)

If this method is called when the composition has already been created it has no effect.

This method should only be called if this view isAttachedToWindow or if a parent CompositionContext has been set explicitly.

For best results in composing while the ComposeView isn't attached, use the version of this with ComposeViewContext as an argument.

@ExperimentalComposeViewContextApi
    fun createComposition(composeViewContext: ComposeViewContext)

Perform initial composition for this view, even if this view isn't attached to the hierarchy. If the view is never attached to the hierarchy, disposeComposition must be called for the composition to be cleaned up properly. If the view is attached to the hierarchy after createComposition, detaching it will clean up the composition, so calling disposeComposition is unnecessary.

If this method is called when the composition has already been created, it will update the composeViewContext being used for the composition.

The composeViewContext values override any previously set setParentCompositionContext value. The composeViewContext values are used for all composition information, including LifecycleOwner, SavedStateRegistryOwner, and window information pulled from the ComposeViewContext's attached View.

Parameters

composeViewContext The ComposeViewContext to use for the composition. The ComposeViewContext.view must be attached to the hierarchy.
fun disposeComposition()

Dispose of the underlying composition and requestLayout. A new composition will be created if createComposition is called or when needed to lay out this view.