Embedding composables in View hierarchies
As you have seen, integrating Views in composable functions is simple and straightforward using AndroidView()
and AndroidViewBinding()
. But what about the other way round? Often, you may not want to rewrite an existing (View-based) app from scratch but migrate it to Jetpack Compose gradually, replacing View hierarchies with composable functions step by step. Depending on the complexity of the activity, it may make sense to start with small composables that reflect portions of the UI and incorporate them into the remaining layout.
The androidx.compose.ui.platform.ComposeView
view makes composables available inside classic layouts. The class extends AbstractComposeView
, which has ViewGroup
as its parent. Once the layout that includes it has been inflated, you can configure the ComposeView
as follows:
binding.composeView.run { setViewCompositionStrategy( ViewCompositionStrategy.DisposeOnDetachedFromWindow...