CoursesCompose Multiplatform, one screen from state to pixels

Puzzle 0 · 1 of 4

Who read the bookmark count?

Tap a bookmark. Work out which part of a shared screen has to run again.

All puzzles in this course

The problem

A reading app has a header that shows the number of bookmarks and a card with a static title. Tapping a bookmark changes the count from 2 to 3. How does Compose know which code needs to run again?

See it

Who read the bookmark count? Count: 2 Header reads 2 Tap: count 3 Header reads 3

Scroll sideways to see the full diagram.

  1. The shared screen starts with two bookmarks.

    Step 1 of 4
  2. The header reads the count. The static title uses its own data.

    Step 2 of 4
  3. The button event changes the count to three. Predict what needs an updated description.

    Step 3 of 4
  4. The header reads three and describes the updated text.

    Step 4 of 4

The building block

A tracked state read is a dependency that Compose records when your code reads an observable value. Here the header reads the bookmark count inside a composable function, a function that describes UI.

@Composable
fun BookmarkCount(count: State<Int>) {
    Text("Bookmarks: ${count.value}")
}

The button event changes the observable count held elsewhere. Compose schedules recomposition, running again the parts of the composition that need to update their UI description. The header reads 3 and describes new text. A sibling with a static title has no dependency on this count. Compose can skip unrelated work when it can reuse the result.

That dependency comes from the read. Holding a reference to the state object and reading its value are different operations. If you read the value at the screen's top and hand an integer down, the read happened at the top, even though the header displays it.

Go deeperMove one read
If the header receives a state object and only reads its value inside the header, where is the observable read?

It takes place in the header. Passing the object from the parent does not by itself read the count.

Back to the main puzzle

Check yourself

Only the count changes. Which UI code depends directly on the count?

Not quite. The tap writes the value. The header reads it. Re-read that part ↑

Not quite. A sibling without the read has no direct dependency. Re-read that part ↑

Right. Locate the observable read.

The parent now reads the integer and passes it to the header. Where does Compose observe that read?

Not quite. Writing the count and observing it are separate. Re-read that part ↑

Not quite. The header receives an integer. The parent read the observable value. Re-read that part ↑

Right. The location of the actual read determines the dependency.

The count is stored only in an ordinary mutable field. What change would let Compose observe edits?

Not quite. Two writes still provide no observable dependency. Re-read that part ↑

Not quite. Moving an ordinary field does not make its reads observable. Re-read that part ↑

Right. Use an observable value and actually read it.

Sources

What you now know

A read of observable state gives Compose a dependency to track. When the value changes, Compose can revisit the parts that read it.