bohregard
05/04/2020, 2:51 AMval context = ContextAmbient.current
Card(modifier = Modifier.padding(8.dp) + Modifier.fillMaxWidth()) {
Button(onClick = {
Log.d(TAG, "Test")
Toast.makeText(context, "Toast me bro", Toast.LENGTH_SHORT)
.show()
}) {
Text(text = "Test")
}
}
}
but this doesn't
Card(modifier = Modifier.padding(8.dp) + Modifier.fillMaxWidth()) {
Button(onClick = {
Log.d(TAG, "Test")
Toast.makeText(ContextAmbient.current, "Toast me bro", Toast.LENGTH_SHORT)
.show()
}) {
Text(text = "Test")
}
}
}
Adam Powell
05/04/2020, 2:57 AMThreadLocal
, but local to your current point in the composition instead. onClick
doesn't run until long after the composition has finished, when the user actually clicks the button. Trying to read ContextAmbient.current
then is kind of like having a background thread try to read a ThreadLocal
that was set on the main thread; they're in scopes that you can't reach across.bohregard
05/04/2020, 2:58 AM