for building native UI. It simplifies and accelerates UI development on Android. Quickly bring your app to life with less code, powerful tools, and intuitive Kotlin APIs.” https://developer.android.com/jetpack/compose
{ val (name, setName) = remember { mutableStateOf("") } … } } • Hold and modify its state internally • Help persist state by remember : from re-compositions rememberSaveable : from con fi guration changes • Caller can use without managing the state themselves
val _name = MutableLiveData("") val name: LiveData<String> = _nam e fun onNameChange(newName: String) { _name.value = newName } } Use ViewModel to hold state and handle event
viewModel()) { val name: String by welcomeViewModel.name.observeAsState("") WelcomeContent(name, onNameChange = { welcomeViewModel.onNameChange(it) }) } Observe value as state in composable function
move the state to the caller by replace the internal state with two parameters value: T value to display onValueChange: (T) -> Unit event when value change @Composabl e fun WelcomeContent ( name: String, onNameChange: (String) -> Unit ) { Column(modifier = Modifier.padding(16.dp)) { Text ( text = "Welcome, $name! to Compose" , … ) OutlinedTextField( onValueChange = { onNameChange(it) } , … ) } }