Hi All
I have a smiliar issue with preserving state. I am using a Lazycolumn in my app and having issues with saving state while scrolling across.
I am trying to save a selection from a group of radio buttons and this is iterative for each set of data. I tried using the following but it retains only what was on display last and loses the rest of the selection
var (selectedOption, onOptionSelected) = rememberSaveable { mutableStateOf(radioOptions[0]) }
Screenshot below comparison after my selection and after a configuration change (Portrait to Lanscape and back)
I tried using rememberSaveableStateHolder() still same issue. Not sure if my usage is correct. Is there anything I am doing wrong?
var saveableStateHolder = rememberSaveableStateHolder()
saveableStateHolder.SaveableStateProvider(radioOptions) {
ChoiceComposable(choice, radioOptions)
}
Following is my code
LazyColumn(
modifier = Modifier
.padding(horizontal = 20.dp, vertical = 30.dp)
.background(Color.White)
) {
items(multiplechoice) {
choice ->
val radioOptions = listOf(choice.choice1,choice.choice2,choice.choice3,choice.choice4,choice.choice5)
QAPage(choice, radioOptions)
}
}
}
@Composable
fun QAPage(choice: QandA,
radioOptions: List<String>
) {
// saving state across each set while scrolling
var saveableStateHolder = rememberSaveableStateHolder()
saveableStateHolder.SaveableStateProvider(radioOptions) {
ChoiceComposable(choice, radioOptions)
}
}
@Composable
fun ChoiceComposable(choice: QandA,
radioOptions: List<String>){
var (selectedOption, onOptionSelected) = rememberSaveable { mutableStateOf(radioOptions[0]) }
Row(
Modifier.rowStyle()
) {
Text(
text = choice.question,
fontSize = 15.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier
.weight(1f)
)
}
radioOptions.forEach { qachoice ->
Column(modifier = Modifier.selectableGroup()) {
// Selection happens in the row section so disabling on click at the radiobutton level
Row(
Modifier.rowStyle()
.selectable(
selected = (qachoice == selectedOption),
onClick = { onOptionSelected(qachoice) },
role = Role.RadioButton
)
) {
Text(
text = qachoice,
fontSize = 7.sp,
modifier = Modifier
.weight(.50f)
.fillMaxHeight()
)
RadioButton(
selected = (qachoice == selectedOption),
onClick = null,
Modifier.radioStyle(),
colors = radiocolors,
)
}
Spacer(
modifier = Modifier
.height(10.dp)
)
}
}
}