Grigorii Yurkov
10/22/2020, 8:49 PM@Composable
fun Test() {
var checked by remember { mutableStateOf(isMyServiceRunning()) }
if (checked) {
startMyService()
} else {
stopMyService()
}
Switch(checked = checked, onCheckedChange = { checked = it})
// ... A lot of others composable functions
}
As far as I understand, one switch will cause recomposition of entire Test()
function, won't it?Halil Ozercan
10/22/2020, 10:25 PMif (checked) {
startMyService()
} else {
stopMyService()
}
this piece of code will be a headache because even though checked
state doesn't change, function still runs. Instead, I would recommend you to accept a lambda into this composable which can be triggered from onCheckedChange
.Leland Richardson [G]
10/23/2020, 12:48 AMchecked
changes, the Test
function will recompose. This means it will get reinvoked, but your intuition for how expensive that will be might not be great. In the example you’ve chosen for instance, the startMyService
stopMyService
and Switch
calls are all skippable, since their parameters don’t change. As a result, the recomposition of Test
will be quite efficientGrigorii Yurkov
10/23/2020, 8:57 AMchecked
variable and not on switch state. In this example only Switch changes checked
, but I can add another Composable functions in future.