'DropdownMenu in separate function doesn't trigger recomposition
I had the following dropdown menu imbedded in a larger composable (columns and rows of some buttons and texts and a lazycolumn of a list of text), and when I select from the menu the list of text in the larger composable refreshes.
Box(
modifier = Modifier
.padding(horizontal = 10.dp)
) {
var expanded by remember { mutableStateOf(false) }
val tags: MutableList<String> = ArrayList(20)
tags.add("All")
if (BaseActivity.tagTokens.isNotEmpty())
tags.addAll(listOf(*BaseActivity.tags()))
Button(onClick = { expanded = !expanded }) {
Text("Tag")
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
) {
tags.forEachIndexed { index, label ->
DropdownMenuItem(onClick = {
expanded = false
addTag(tags[index])
}) {
Text(text = label)
}
}
}
}
Then I moved this block to an outside function in a separate Kotlin file:
@Composable
fun TagSpinner(addTag : (String) -> Unit) {
Box(
modifier = Modifier
.padding(horizontal = 10.dp)
) {
var expanded by remember { mutableStateOf(false) }
val tags: MutableList<String> = ArrayList(20)
tags.add("All")
if (BaseActivity.tagTokens.isNotEmpty())
tags.addAll(listOf(*BaseActivity.tags()))
Button(onClick = { expanded = !expanded }) {
Text("Tag")
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
) {
tags.forEachIndexed { index, label ->
DropdownMenuItem(onClick = {
expanded = false
addTag(tags[index])
}) {
Text(text = label)
}
}
}
}
}
Then in the larger composable at the same place I call this function:
TagSpinner(addTag = ::addTag)
But this time, after I select from the menu, with debug, I see codes are executed in the same way, except that the composable of the text list is not called, so the larger composable is not refreshed. Any idea why?
Solution 1:[1]
Notice that Box is an inline function, TagSpinner is not inline,For inline functions, because they are expanded at compile time at the call point, the appropriate call entry cannot be found at the next reorganization and the caller's reorganization scope can only be shared.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yshh |
