ComposeViewContext
-
android
class 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(Constructs a |
android
|
Public functions |
||
|---|---|---|
ComposeViewContext |
copy(Construct a |
android
|
Public constructors
ComposeViewContext
ComposeViewContext(
view: View,
compositionContext: CompositionContext? = null,
lifecycleOwner: LifecycleOwner? = null,
savedStateRegistryOwner: SavedStateRegistryOwner? = null,
viewModelStoreOwner: ViewModelStoreOwner? = null
)
Constructs a ComposeViewContext to be used with AbstractComposeView.createComposition to compose content while the AbstractComposeView isn't attached.
| Parameters | |
|---|---|
view: View |
A |
compositionContext: CompositionContext? = null |
The |
lifecycleOwner: LifecycleOwner? = null |
Used to govern the lifecycle-important aspects of |
savedStateRegistryOwner: SavedStateRegistryOwner? = null |
The |
viewModelStoreOwner: ViewModelStoreOwner? = null |
|
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 |
compositionContext: CompositionContext? = this._compositionContext |
The |
lifecycleOwner: LifecycleOwner? = this._lifecycleOwner |
Used to govern the lifecycle-important aspects of |
savedStateRegistryOwner: SavedStateRegistryOwner? = this._savedStateRegistryOwner |
The |
viewModelStoreOwner: ViewModelStoreOwner? = this._viewModelStoreOwner |
|