Suppose we have the following code. It displays a button, and when the user clicks on it, the button disappears.
@Composable
fun ButtonThatDisappearsOnClick() {
var showButton by remember { mutableStateOf(true) }
if (showButton) {
Button(onClick = {
check(showButton) { "onClick shouldn't be called for a hidden button" } // !!!
showButton = false
}) {
Text("My button")
}
}
}
I suspect that the check
call above may fail if the user clicks the button twice really quickly:
- The user clicks the button,
shouldShowButton
is set tofalse
. Since the value in a mutable state was updated, a recomposition is scheduled. - The user clicks the button very quickly again before the views have been recomposed. Thus, the
onClick
function will fire the second time, and thecheck
call will fail.
I have not been able to reproduce this in practice, so I am wondering if such a behavior is indeed possible.