I have a LazyColumn
containing ToggleButtonGroups that I have created myself. My issue can be reproduced by these three steps:
The problem does not appear if I don't scroll after rotating. So by rotation alone, the state is saved properly, as I would have expected it by using rememberSaveable
.
The code is provided below:
LazyColumn() {
items(characteristics) {characteristic: Characteristic ->
ToggleButtonGroup(
defaultIndex = 2,
values = listOf(Pair("YES", -1), Pair("NO", 1), Pair("TBD", 0)),
onToggleChange = { newValue: Int ->
characteristic.value = newValue
}
)
}
}
The Composable named ToggleButtonGroup
is seen below:
@Composable
fun ToggleButtonGroup(defaultIndex: Int, values: List<Pair<String, Int>>, onToggleChange: (newValue: Int) -> Unit) {
var selectedIndex by rememberSaveable {
mutableStateOf( defaultIndex.coerceIn(0, values.size - 1) )
}
Row(
modifier = Modifier.wrapContentHeight()
) {
values.forEachIndexed { index: Int, value: Pair<String, Int> ->
FilledTonalButton(
colors = if (selectedIndex == index) ButtonDefaults.buttonColors() else ButtonDefaults.filledTonalButtonColors(),
onClick = { selectedIndex = index; onToggleChange(value.second) },
shape = RectangleShape
) {
Text(text = value.first)
}
}
}
}
And the characteristics
data is coming from my ViewModel:
data class Characteristic(val title: String, val weight: Int, var value: Int)
var characteristics by mutableStateOf(listOf<Characteristic>())
Thank you for any efforts!