AbstractComposeView
abstract class AbstractComposeView
@JvmOverloads
constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
ViewGroup(context, attrs, defStyleAttr)
Base class for custom android.view.View
s 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
@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."
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.
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.