Handling user actions
In the previous section, we learned how to build user interfaces using Jetpack Compose. In the exercise, we were unable to collect the data that the user sets in TextField
. In this section, we will learn how to handle the user input as well as the state of the user interface.
Let’s assume we have the following example:
@Composable fun MyScreen() { Column { TextField(value = "", onValueChange = {}) } }
In this example, we define a TextField
that is empty and has no handling for the change of the value. As we’ve seen, this won’t let us introduce any new input from the keyboard because it will always set the text to an empty string.
For us to be able to introduce a new text, we will need to create a mutable variable to store the text inside and for it to survive recomposition. In Jetpack Compose, we can use the @Composable
function called remember
to define a MutableState
that will hold our text: