Jetpack Compose

Bites of Compose 3

Learn Jetpack Compose one bite at a time

Guowei Lv

4 minute read

Let’s do a bit of recap first. (I highly suggest you go through previous post if not already done so) Situation 1 Will clicking the button trigger the recompose of UserPage? data class User(var name: String) class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val user1 = User("Guowei Lv") val user2 = User("Guowei Lv") var user = user1 setContent { var flag by remember { mutableStateOf(true) } Column { Text(text = "Flag is $flag") UserPage(user = user) Button(onClick = { flag = !

Bites of Compose 2

Learn Jetpack Compose one bite at a time

Guowei Lv

5 minute read

Situation 1 Take a look at the Composable below, what will happen if the button is clicked? @Composable fun Situation1() { val names by remember { mutableStateOf(mutableListOf("Bob", "Tom")) } Column { names.forEach { Text(it) } Button(onClick = { names.add("Jane") }) { Text("Add Jane!") } } } Answer The list will still consist of Bob and Tom. Jane will not be added. The reason is that for mutable states, only the assignment operation is “observed”.

Bites of Compose 1

Learn Jetpack Compose one bite at a time

Guowei Lv

2 minute read

I’m planning to write a series of short articles about Android’s Jetpack Compose UI framework. You can use it to test your understanding or maybe learn a few things along the way. Situation 1 Take a look at the Composable below, what will happen if the button is clicked? @Composable fun Situation1() { var name = "Guowei" Column { Text(name) Button(onClick = { name = "Hello" }) { Text("Change name!") } } } Answer Nothing.