Interview frequency: …
Short answer
Compose is a declarative framework that describes the UI based on the current state, while the old imperative approach relies on modifying the View tree by hand.
Explanation
In the traditional Views system (imperative), you initialize the View and modify its properties manually, such as textView.setText() or visibility = GONE, which may cause inconsistency between the displayed data and the actual state. In Jetpack Compose, you describe the UI as a function that receives the state, and when the state changes Compose re-invokes the affected functions automatically (recomposition).
Code example
@Composable
fun Greeting(name: String, isVisible: Boolean) {
if (isVisible) {
Text(text = "Welcome, $name!", style = MaterialTheme.typography.bodyLarge)
}
}Common mistakes
- Trying to keep a reference to a composable function or modify its properties after it has run, as was done with old View objects.