What you'll learn
Composables become powerful when they take parameters — then one function can render any data you pass it, and you build a whole screen by composing small, reusable pieces. This module also introduces recomposition, the engine that keeps your UI in sync with your data.
By the end you'll be able to:
- Pass parameters to composables to make them reusable
- Compose screens from smaller composables
- Explain what recomposition is
- Configure previews for different scenarios
Parameters
A composable takes parameters exactly like any Kotlin function. Passing data in makes it reusable — instead of a composable hard-coded to one name, you get one that renders any name. Default values work too, so callers can omit optional parameters.
Reusing composables
This is how real UIs are built: define a small composable once, then call it repeatedly with different data. Here a ProfileCard is reused to build a team list — and notice a composable (Team) is made entirely of calls to other composables:
@Composable
fun ProfileCard(name: String, role: String) {
Column {
Text(text = name)
Text(text = role)
}
}
@Composable
fun Team() {
Column {
ProfileCard(name = "Ada", role = "Engineer")
ProfileCard(name = "Linus", role = "Maintainer")
}
}Key idea
Screen calls a Header and a List, the list calls many Rows, and so on. Compose it like Lego, from small blocks up.Recomposition
When observable state read during composition changes, Compose schedules affected composition work to update the screen — this is called recomposition. You never manually update the UI; you change the data, and Compose updates the parts that depend on it. Compose can skip eligible calls when their inputs have not changed. Skipping is an optimization, not a promise that a function executes exactly once per change.
Note
Configured previews
Previews take parameters to simulate real conditions. Stack several on one composable to check it in light and dark themes, at different font sizes, or on different screen widths — all without running the app:
import android.content.res.Configuration
@Preview(name = "Light", showBackground = true)
@Preview(
name = "Dark",
showBackground = true,
uiMode = Configuration.UI_MODE_NIGHT_YES,
)
@Composable
fun ProfileCardPreview() {
MyAppTheme {
ProfileCard(name = "Ada", role = "Engineer")
}
}Practice: one small working feature
Give a greeting composable a name parameter. Call it with two different names in two previews.
Check your result
Both previews use one implementation and show their own supplied names.
Remember
Recap & quick check
Key takeaways
- Composables take parameters like any function, which makes them reusable with different data.
- Build screens by composing small composables into a tree — call composables inside composables.
- Recomposition updates affected UI when observable state read during composition changes.
- Compose can skip eligible calls with unchanged inputs; do not depend on an exact execution count.
- Keep composable bodies fast and side-effect-free — they may re-run frequently.
Quick check
1. Why pass parameters to a composable?
2. What is recomposition?
3. How does Compose keep recomposition efficient?
4. Why should a composable body avoid side effects?
5. How do you preview a composable in both light and dark themes?
You can build reusable UI now. Next we style it — mastering Text and the Modifier system that shapes every composable.