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.
Androidx.compose.ui.platform.ComposeView
makes composables available inside classic layouts. The class extends AbstractComposeView
, which has ViewGroup
as its parent. Once the layout that includes the ComposeView has been inflated, here is how you configure it:
binding.composeView.run { setViewCompositionStrategy( ViewCompositionStrategy.DisposeOnDetachedFromWindow...