Your First Kotlin Android App: An App From Scratch

Aug 15 2023 · Kotlin 1.8.20, Android 13, Android Studio Flamingo | 2022.2.1

Part 2: Manage Data in Jetpack Compose

14. Understand State in Jetpack Compose

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 13. Handle a Click Interaction Next episode: 15. Learn About State Hoisting

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

A key part of programming in Jetpack Compose is state. Rather than start with the computer science definition of state, let’s go with something that might be a little more familiar: the dashboard of a car.

var alertIsVisible by remember { mutableStateOf(false) }
@Composable
fun ResultDialog(
  modifier: Modifier = Modifier
) {
  AlertDialog(
    onDismissRequest = {},
    confirmButton = {
      TextButton(
        onClick = {}
      ) {
        Text(stringResource(id = R.string.result_dialog_button_text))
      }
    },
    title = { Text(stringResource(id = R.string.result_dialog_title)) },
    text = { Text(stringResource(id = R.string.result_dialog_message)) }
  )
}
ResultDialog()
@Composable
fun ResultDialog(
  hideDialog: () -> Unit, // New Code
  modifier: Modifier = Modifier
)
  //...
onDismissRequest = {
  hideDialog() // New Code
},
//...
    TextButton(
      onClick = {
        hideDialog() // New Code
      }
    )
//...
ResultDialog(
  hideDialog = { alertIsVisible = false }
)