Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Building Screens with Jetpack Compose  ›  Lesson

TextField and remember

TextField 15 minA TextField shows the value you give it and reports edits through onValueChange, so its text must be held in state that you update.

TextField and remember

Sprout's add-plant screen has a name field. A first attempt often looks like this, and typing into it does absolutely nothing:

TextField(value = "", onValueChange = { })

Nothing is broken. The field is doing exactly what it was told. Understanding why is the key to all Compose input.

The rule

The field shows your state and reports edits; you write them back.

A TextField does not own its text. It has two halves:

  • value — what to display. It always shows exactly this.
  • onValueChange — called with the new text when the user types. It is a request, not a change.

So you need state that holds the text, survives recomposition, and triggers a redraw when it changes:

var name by remember { mutableStateOf("") }

TextField(
    value = name,
    onValueChange = { name = it },
    label = { Text("Plant name") }
)
  • mutableStateOf("") creates observable state: when it changes, readers recompose.
  • remember keeps that state across recompositions. Without it, each recomposition would create a fresh empty state.
  • rememberSaveable does the same and also survives rotation, as you saw in the lifecycle section. For typed text, prefer it.

Because the value passes through your code, you can also shape it, for example onValueChange = { name = it.take(30) } to cap the length.

When state is kept in a parent and passed down as value plus a callback, the child is stateless and easy to reuse and preview. This pattern is called state hoisting.

The shortcut

Trace one keystroke as a loop: type → report → write → redraw.

Typing "M" into Sprout's name fieldStep 1 of 4
  1. Type

    The user presses M. The field does not change yet.

If the loop is broken anywhere (no state, no remember, or an empty callback), the field appears frozen.

Drills

Answer fast — trust the rule.

quick drill

Why does TextField(value = "", onValueChange = { }) never show typed text?

quick drill

What does remember do in remember { mutableStateOf("") }?

quick drill

Which line correctly updates the state from the field?

quick drill

Sprout's name text should survive rotation. Which should you use?

Remember

  • The field shows your state and reports edits; you write them back.
  • remember { mutableStateOf(...) } holds it; rememberSaveable also survives rotation.
  • A frozen field means the type → report → write → redraw loop is broken.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainThe field that will not type

A teammate writes TextField(value = "", onValueChange = {}) for Sprout's plant name, and typing does nothing. Explain why, and describe the fixed code including how the value is remembered.