A large View-based Android app rarely moves to Jetpack Compose in one rewrite. More often the migration runs for months: new screens are written in Compose, old screens keep getting features, and many screens contain both. Compose was designed for this. You can put Compose inside a View hierarchy, put a View inside Compose, and move one screen at a time while the app keeps shipping.
Most of the work is at the seams between the two toolkits, so that is what this post covers: the interop APIs, theming, state, the usual pitfalls, testing and a sensible migration order.
Two directions of interop
ComposeView: Compose inside the View world
ComposeView is a regular Android View that hosts a composition. You can declare it in XML and set its content from a Fragment or Activity, which makes it the main tool when you migrate a screen piece by piece: first the header, then the list, then the whole screen.
<androidx.compose.ui.platform.ComposeView
android:id="@+id/compose_header"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
class ProfileFragment : Fragment() {
private var _binding: FragmentProfileBinding? = null
private val binding get() = _binding!!
private val viewModel: ProfileViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentProfileBinding.inflate(inflater, container, false)
binding.composeHeader.apply {
// Tie the composition to the Fragment's view lifecycle.
setViewCompositionStrategy(
ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed
)
setContent {
AppTheme {
ProfileHeader(viewModel)
}
}
}
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
The composition strategy is the line that matters here. The default, DisposeOnDetachedFromWindowOrReleasedFromPool, disposes the composition when the ComposeView is detached from the window (pooling containers are a special case, covered below). That is right for a Compose-only app, but the official docs note it can lose state when you add Compose incrementally. In a Fragment you want the composition to live exactly as long as the Fragment's view. DisposeOnViewTreeLifecycleDestroyed does that: it disposes the composition when the view tree's LifecycleOwner is destroyed, and for a Fragment that owner is viewLifecycleOwner.
If one layout contains several ComposeViews, give each a unique id; otherwise saved instance state does not work for them.
AndroidView: a View inside Compose
The other direction matters just as much. A screen that is already mostly Compose may still need a widget you have not rewritten yet: a custom chart, a map, a signature pad. The AndroidView composable hosts it:
@Composable
fun LegacyChart(points: List<DataPoint>, modifier: Modifier = Modifier) {
AndroidView(
modifier = modifier.fillMaxWidth(),
factory = { context ->
// Create the view and do one-time setup.
LineChartView(context).apply {
axisStyle = AxisStyle.Minimal
}
},
update = { view ->
// Runs after factory, and again when the values it reads change.
view.submitPoints(points)
}
)
}
factory creates the view once and is the place for setup that never changes. update runs after the view is created and again whenever the state it reads changes. If you create a new view inside update, you rebuild it on every update; if you put state-dependent calls only in factory, the view never sees new values. When a change really requires a fresh instance, wrap the call in key(...) { } instead of working around update.
The plain factory/update overload keeps one view instance for as long as the composable stays in the composition. Since Compose UI 1.4 there is also an overload with onReset and onRelease. When onReset is non-null, Compose can reuse the view inside lazy layouts (LazyColumn, LazyRow, Pager): onReset clears the previous item's state before update runs for the next one, and onRelease is called when the view leaves the composition for good. Use that overload when an AndroidView sits inside a lazy list. The Views in Compose guide covers both overloads.
AndroidViewBinding and AndroidFragment
If the legacy piece is a whole XML layout rather than a single view, AndroidViewBinding from androidx.compose.ui:ui-viewbinding inflates it through its generated binding class (view binding must be enabled):
@Composable
fun LegacyAddressForm(onSave: () -> Unit) {
AndroidViewBinding(LegacyAddressFormBinding::inflate) {
saveButton.setOnClickListener { onSave() }
}
}
The trailing lambda plays the role of update, with the binding as the receiver. Use it for smaller legacy layouts during the migration, not to wrap entire screens. To embed a whole Fragment in Compose, there is the AndroidFragment composable, which removes the Fragment when the composable leaves the composition.
One design system, two renderers
During the migration your colors, typography and shapes are drawn by two systems: the XML theme (Material Components) and Compose's MaterialTheme. If they drift apart, a user sees a slightly different primary color or text style the moment content switches from a View to a ComposeView on the same screen.
The old runtime bridges (MdcTheme/Mdc3Theme from the theme adapter libraries) are deprecated, so don't build on them. Treat the existing XML theme values as the source of truth and define your Compose AppTheme from the same values: write it by hand so it mirrors the XML, or use Material Theme Builder, which can export both an XML theme and a Compose theme from the same input. Then wrap every Compose entry point (every setContent, every new screen) in AppTheme. A composition without it falls back to Compose's default Material colors.
It is the same idea I used when splitting an eID SDK into reading, verification and integration layers: each side stays behind a clear boundary, so you can change one without breaking the other.
Fragments and Navigation
Most incremental migrations happen inside an existing Fragment + Navigation setup. The simplest way forward is to keep the Fragment-based navigation graph and let more and more destinations be Fragments whose content is a ComposeView. Navigation Compose becomes an option once every destination in the graph is a composable; the screens themselves can still contain some View content. Until then, avoid splitting "where do we go next" between a Fragment graph and a Compose NavHost. One layer owns navigation, the other only emits events.
Back handling is another seam to decide on purpose. In a hybrid screen, choose whether the Fragment or the composable intercepts the system back gesture, especially with predictive back. The approach from the move from onBackPressed to OnBackPressedDispatcher applies here too: one owner, bound to the right lifecycle.
Hoisting state at the boundary
A half-migrated screen stays consistent when there is one source of UI state. The ViewModel exposes a single state stream, and both the Compose part and the remaining View part read from it.
@Composable
fun ProfileHeader(viewModel: ProfileViewModel) {
val state by viewModel.uiState.collectAsStateWithLifecycle()
ProfileHeaderContent(
state = state,
onEditClick = viewModel::onEditClicked
)
}
collectAsStateWithLifecycle comes from androidx.lifecycle:lifecycle-runtime-compose and uses repeatOnLifecycle under the hood: collection stops when the lifecycle drops below STARTED (by default) and resumes when it comes back. Inside a Fragment's ComposeView, that lifecycle is the Fragment's view lifecycle. It is the Compose counterpart of the lifecycle-aware Flow collection I used when moving a project from RxJava to Kotlin Coroutines.
The View side of the same Fragment collects the same flow:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collect { state ->
binding.followButton.isEnabled = !state.isLoading
}
}
}
}
The Fragment passes its own viewModel into ProfileHeader. Calling viewModel() inside the ComposeView would return the same Fragment-scoped instance, but passing it explicitly makes the shared owner obvious.
ProfileHeaderContent is stateless: state goes down, events go up. With a thin stateful wrapper at the boundary and stateless content underneath, the content composable can be reused as is when the whole screen moves to Compose later.
In the other direction, the update block of an AndroidView is the boundary: read the hoisted state there and push it into the legacy view's setters.
Common pitfalls
Theme drift
Often the most visible bug in a hybrid app. Symptoms: ripple color, elevation or font weight changes when content crosses a View/Compose boundary. Cause: a composition that is not wrapped in AppTheme, or an AppTheme that no longer matches the XML values. The fix is the one above: one set of values, every entry point wrapped.
Nested scrolling
A LazyColumn in a ComposeView inside a CoordinatorLayout with a collapsing toolbar will not move the toolbar on its own. Compose supports nested scrolling, but to pass scroll events up to a View parent, that parent must implement NestedScrollingParent3 and the Compose side needs rememberNestedScrollInteropConnection() passed to Modifier.nestedScroll:
setContent {
AppTheme {
val nestedScroll = rememberNestedScrollInteropConnection()
LazyColumn(
modifier = Modifier.nestedScroll(nestedScroll)
) {
items(feedItems, key = { it.id }) { item ->
FeedRow(item)
}
}
}
}
CoordinatorLayout implements NestedScrollingParent3, so this works there. RecyclerView and ViewPager2 do not, so the connection won't give you one continuous scroll from a Compose child into those parents. In that case, change which component owns the scrolling instead of forcing it.
ComposeView inside RecyclerView
Using a ComposeView as a RecyclerView item is a common intermediate step. The default strategy, DisposeOnDetachedFromWindowOrReleasedFromPool, already handles this case: inside a pooling container the composition is not disposed on every detach, only when the RecyclerView itself leaves the window or the item is discarded from the pool. With current Compose and RecyclerView versions, the older advice to call disposeComposition() in onViewRecycled is no longer needed. Keep the default here, and use DisposeOnViewTreeLifecycleDestroyed for ComposeViews that belong to a Fragment's view.
Many small ComposeViews
Every ComposeView is its own composition with its own setup cost. Dozens of tiny ComposeViews scattered through a legacy layout can cost more than one larger Compose area. Prefer fewer, larger Compose sections, and measure with the profiler before deciding a boundary is too expensive.
Testing the hybrid screen
Compose tests and Espresso work together in the same test. createAndroidComposeRule launches a real Activity; you find composables through the rule and Views with Espresso's onView, with no special setup:
class ProfileScreenTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<ProfileActivity>()
@Test
fun editClick_showsEditForm() {
composeTestRule.onNodeWithText("Edit profile").performClick()
onView(withId(R.id.edit_profile_form))
.check(matches(isDisplayed()))
}
}
Stateless composables like ProfileHeaderContent are easier still: createComposeRule() plus setContent lets you test each state without the Fragment or the ViewModel. Having UI tests on a screen before you migrate it is the simplest way to catch regressions along the way.
Where to start
The order the official migration strategy recommends is a good default:
- Write new screens and features in Compose.
- Set up the shared theme and build common components as you go, so each is defined in one place.
- Replace existing screens one at a time, starting with simple ones (settings, confirmation, mostly static content). On mixed screens, migrate bottom-up, subtree by subtree.
- Move navigation last.
Throughout, keep each screen's state in a ViewModel that both sides read, and match each ComposeView's strategy to its host.