0
0
Android Kotlinmobile~20 mins

Compose with existing Views (interop) in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Compose Interop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this Compose interop code?
Consider this Compose code embedding a traditional Android Button inside Compose. What will the user see and experience?
Android Kotlin
setContent {
  AndroidView(factory = { context ->
    Button(context).apply {
      text = "Click me"
      setOnClickListener { Toast.makeText(context, "Clicked!", Toast.LENGTH_SHORT).show() }
    }
  })
}
AA Compose Button with text 'Click me' but no click action happens on tap.
BA runtime crash because Button cannot be used inside Compose.
CA blank screen because AndroidView does not render inside Compose.
DA Compose UI showing a native Android Button labeled 'Click me' that shows a toast 'Clicked!' when tapped.
Attempts:
2 left
💡 Hint
AndroidView lets you embed classic Android Views inside Compose UI.
lifecycle
intermediate
2:00remaining
What happens to the Android View lifecycle inside AndroidView in Compose?
When you use AndroidView in Compose to embed a View, which lifecycle event is guaranteed to be called when the Compose composable leaves the composition?
AThe View remains attached and no lifecycle events are called.
BThe View's onDestroy() method is called automatically.
CThe View's onDetachedFromWindow() is called automatically.
DThe View's onPause() method is called automatically.
Attempts:
2 left
💡 Hint
AndroidView manages the View lifecycle according to Compose's composition lifecycle.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly updates the Android View inside AndroidView when Compose state changes?
You want to update the text of an Android TextView inside AndroidView when a Compose state variable changes. Which snippet correctly does this?
Android Kotlin
var text by remember { mutableStateOf("Hello") }
AndroidView(factory = { context -> TextView(context) }, update = { it.text = text })
AAndroidView(factory = { context -> TextView(context) }, update = { it.text = text })
BAndroidView(factory = { context -> TextView(context).apply { text = text } })
CAndroidView(factory = { context -> TextView(context) }, update = { it.setText("Hello") })
DAndroidView(factory = { context -> TextView(context) }, update = { it.text = "Hello" })
Attempts:
2 left
💡 Hint
Use the update parameter to react to Compose state changes.
🔧 Debug
advanced
2:00remaining
Why does this AndroidView code cause a memory leak?
This code embeds a WebView inside Compose using AndroidView. Why might it cause a memory leak? AndroidView(factory = { context -> WebView(context) })
ABecause WebView holds references to the context and is not properly destroyed when Compose disposes the AndroidView.
BBecause AndroidView does not support WebView and crashes immediately.
CBecause WebView requires a Fragment, not a Context, causing errors.
DBecause Compose automatically destroys WebView causing null pointer exceptions.
Attempts:
2 left
💡 Hint
WebView needs explicit cleanup to avoid leaks.
🧠 Conceptual
expert
2:00remaining
What is the main benefit of using Compose with existing Views via AndroidView?
Why would a developer choose to embed existing Android Views inside Compose using AndroidView instead of rewriting the UI fully in Compose?
ABecause Compose cannot create any UI elements on its own.
BTo reuse existing complex Views or libraries without rewriting, enabling gradual migration to Compose.
CBecause AndroidView automatically converts Views to Compose components for better performance.
DBecause Compose requires all Views to be wrapped in AndroidView to display anything.
Attempts:
2 left
💡 Hint
Think about migration and reuse of legacy code.