Compose and Views together

Jetpack Compose Mid-level Kotlin 2.0+ API 24+ Reviewed By Best Latest Apps

Pick the fix

A WebView inside Compose reloads on every recomposition. What's wrong?

Options
Hint 1 of 1

Which lambda runs once?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A The WebView is created in update; create it in factory and only change it in update

Correct. factory runs once; update runs every recomposition.

Why not the others?
  • B AndroidView can't host WebView. It can; that's what it's for.
  • C Add remember around the url. The url isn't the problem; the View is recreated.
  • D Use LaunchedEffect to load the page. That doesn't stop the View being recreated.

30-second answer

Put a ComposeView in the XML or Fragment and call setContent { } on it. Set a ViewCompositionStrategy that matches the owner, such as DisposeOnViewTreeLifecycleDestroyed in Fragments, so the composition is disposed with the view. To host a View inside Compose, use AndroidView(factory = { }, update = { }).

Practise saying it · 30 s. No recording, just a timer

Explain Compose and View interop.

Say your answer out loud, then check which of these points you covered:

  • ComposeView with setContent
  • Composition strategy for Fragments
  • AndroidView factory and update
  • Migrate gradually

Deep dive

factory runs once to create the View; update runs on each recomposition with new state.

Wrap your Material theme for Compose so both systems look the same during migration.

Fragments can return a ComposeView from onCreateView for Compose-only screens.

Heavy Views such as MapView or WebView keep their own lifecycle; forward it or use the library's Compose wrapper.

Compose inside a Fragment Compile-checked · Kotlin 2.4.21
class NotesFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
        ComposeView(requireContext()).apply {
            setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
            setContent { Text("Hello from Compose") }
        }
}

Common mistakes

  • Creating the View inside update on every recomposition.
  • Forgetting the composition strategy in a Fragment, so the composition's lifetime doesn't follow the Fragment's view.
  • Expecting View state to be saved automatically inside AndroidView.

Senior take & follow-ups

Mention migration strategy, not just the APIs: start with leaf components or new screens.

Follow-up questions

Think of your answer first, then open the hint.

  1. Where should a new View be created in AndroidView?

    Show hint

    In factory, once.

  2. What disposes the composition in a Fragment?

    Show hint

    DisposeOnViewTreeLifecycleDestroyed, tied to the view lifecycle.

Compare on iOS: UIViewRepresentable feedback loops UIViewRepresentable's make/update match AndroidView's factory/update.

Suggest a fix if something here is wrong or out of date.