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

15. Learn About State Hoisting

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: 14. Understand State in Jetpack Compose Next episode: 16. Work with Strings

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

In the previous episode, you triggered the update of the alertIsVisible state object that is defined in the GameScreen composable from an event inside the alert dialog.

import androidx.compose.runtime.Composable

@Composable
fun TargetSlider(modifier: Modifier = Modifier) {

}
@Composable
fun TargetSlider(modifier: Modifier = Modifier) {
  Row(
    verticalAlignment = Alignment.CenterVertically,
    modifier = modifier // Make sure you add this
  ) {
    Text(
      stringResource(id = R.string.min_value_text),
      textAlign = TextAlign.Center,
      modifier = Modifier.padding(start = 16.dp)
    )
    Slider(
      value = 0.5f,
      valueRange = 0.01f..1f,
      onValueChange = { },
      modifier = Modifier.weight(1f)
    )
    Text(
      stringResource(id = R.string.max_value_text),
      textAlign = TextAlign.Center,
      modifier = Modifier.padding(end = 16.dp)
    )
  }
}
TargetSlider()
fun TargetSlider(modifier: Modifier = Modifier, value: Float = 0.5f) {
  //...
}
Slider(
  value = value,
  //...
)
@Composable
fun TargetSlider(
  modifier: Modifier = Modifier
  value: Float = 0.5f,
  valueChanged: (Float) -> Unit, // New Code
) {
  //...
}
onValueChange = valueChanged,
var sliderValue by remember { mutableStateOf(0.5f) }
TargetSlider(
  value = sliderValue,
  valueChanged = { value ->
    sliderValue = value
  }
)