Kotlin Multiplatform, from the compiler up · Puzzle 13 · 14 of 16
Compose in one puzzle
The last module shares the UI itself, with Compose. If you've built screens with Activities and XML, here's the one idea Compose is built on, and the compiler step that makes it work, before we look at what can be shared.
The problem
With XML layouts, you build a view once, then change it by hand: textView.text = name. Compose has no such step: you write a function, and the screen follows your data. How does the screen update, if nothing tells it to?
See it
A composable: a function that describes the UI for its data. For "Ana", the screen shows "Hello, Ana".
Step 1 of 4The name changes to "Ben". It's state, kept across calls with remember.
Step 2 of 4Compose calls
Greetingagain with "Ben": recomposition. Predict: how does it know what to redo?Step 3 of 4The Compose compiler plugin rewrote the function with hidden bookkeeping, so the runtime redoes only what changed.
Step 4 of 4
The building block
A composable is a Kotlin function marked @Composable that takes data and describes the UI for it:
@Composable
fun Greeting(name: String) {
Text("Hello, $name")
}
It returns nothing and creates no view objects you keep. It describes what the screen should look like for this name.
When the data changes, Compose calls the function again with the new value: recomposition. It's smart about it: only the functions whose inputs might have changed run again; the rest are skipped. Data that should trigger it is state, for example var count by remember { mutableStateOf(0) }; remember keeps that value across recompositions, so it isn't reset each time the function runs.
How can the runtime know which functions showed what? That's the Compose compiler plugin, a part of the Kotlin compiler from puzzle 0. It rewrites every @Composable function, adding hidden bookkeeping so Compose can track what each call showed and skip what didn't change.
The analogy
A weather board that a printer reprints whenever the forecast changes, instead of a person climbing up to swap single letters. You only ever write what the board should say; the printer handles the rest, and only reprints the panels that changed.
Check yourself
Sources
- Android Developers: Thinking in Compose ("you can build your user interface by defining a set of composable functions that take in data and emit UI elements"; "This annotation informs the Compose compiler that this function is intended to convert data into UI"; "Recomposition is the process of calling your composable functions again when inputs change. When Compose recomposes based on new inputs, it calls only the functions or lambdas that might have changed, and skips the rest.")
What you now know
In Compose, a screen is a Kotlin function marked @Composable that describes the UI for the current data, instead of creating and updating view objects. When that data, its state, changes, Compose calls the affected functions again: recomposition. remember keeps a value across those calls. A compiler plugin rewrites every @Composable function so the runtime can track what each one showed and redo only what changed.
Answer every question above correctly to unlock the next puzzle.
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.