Mark Murphy
04/08/2021, 11:50 PMMark Murphy
04/08/2021, 11:52 PM@Composable
private fun RecomposeTest() {
val switchState = remember { mutableStateOf(false) }
Log.d("wut?", "RecomposeTest()")
Row(modifier = Modifier.padding(8.dp)) {
Switch(
checked = switchState.value,
onCheckedChange = { switchState.value = it },
modifier = Modifier.padding(end = 4.dp)
)
Text(stringResource(R.string.app_name))
}
}
I had expected that only the Switch()
would get recomposed when the Switch()
itself is clicked, as the call to Switch()
is the only place where I seem to reference that switchState
value.
Yet, I get the log message on every click of the Switch()
. Unless I am misunderstanding something, that means that RecomposeTest()
is being recomposed, not just the Switch()
, or even the Row()
.
FWIW, I see this with beta01
and beta04
.Mark Murphy
04/09/2021, 12:02 AMRecomposeTest()
is being called from the setContent()
call in the activity:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
// A surface container using the 'background' color from the theme
Surface(color = MaterialTheme.colors.background) {
RecomposeTest()
}
}
}
}
}
Otherwise, the project is standard Arctic Fox Canary 12 "Empty Compose Activity", except for bumping things up to beta04
after realizing that the template was at beta01
.Mark Murphy
04/09/2021, 12:07 AMLog.d()
call being made for every Switch()
click?
The more interesting question is: how do folks like me figure out what is going on here? IOW, is there a way that I can determine why the Compose runtime is deciding that RecomposeTest()
needs to be called again?Sean McQuillan [G]
04/09/2021, 12:07 AMRow
is inlineSean McQuillan [G]
04/09/2021, 12:08 AMSean McQuillan [G]
04/09/2021, 12:08 AMchecked = switchState.value
and the read happens there (when resolving the function parameters)Mark Murphy
04/09/2021, 12:09 AMRow()
being inline
, that's actually in the body of RecomposeTest()
🤦🏻♂️Sean McQuillan [G]
04/09/2021, 12:09 AMMark Murphy
04/09/2021, 12:11 AMSean McQuillan [G]
04/09/2021, 12:11 AMMark Murphy
04/09/2021, 12:12 AMSean McQuillan [G]
04/09/2021, 12:13 AMSean McQuillan [G]
04/09/2021, 12:13 AMSean McQuillan [G]
04/09/2021, 12:13 AMSean McQuillan [G]
04/09/2021, 12:14 AMSean McQuillan [G]
04/09/2021, 12:15 AMval aColorForSomeReason = if (switchedState.value) { Color.Red } else { Color.Blue }
Mark Murphy
04/09/2021, 12:16 AMSean McQuillan [G]
04/09/2021, 12:16 AMSean McQuillan [G]
04/09/2021, 12:16 AMSean McQuillan [G]
04/09/2021, 12:16 AMMark Murphy
04/09/2021, 12:17 AMRow()
?Sean McQuillan [G]
04/09/2021, 12:17 AMSean McQuillan [G]
04/09/2021, 12:17 AMSean McQuillan [G]
04/09/2021, 12:18 AMMark Murphy
04/09/2021, 12:19 AMZach Klippenstein (he/him) [MOD]
04/09/2021, 2:15 AMjulioromano
04/09/2021, 7:33 AMMark Murphy
04/09/2021, 1:04 PMI do call out the inline caveat at the end of the postI saw that, both before and after I started this thread. In my case, it was mostly a matter of "not putting two and two together" with what you had there and the symptoms that I was seeing. IOW, 🤦🏻♂️ . Again, the post was great!
Zach Klippenstein (he/him) [MOD]
04/09/2021, 3:52 PM