Hello! Maybe somebody could help understand why `r...
# compose
d
Hello! Maybe somebody could help understand why
rememberSaveable
doesn’t keep state And what I need to do to fix that
a
It looks like you might be looking for
SaveableStateHolder
?
a
That's actually happens because of using if statement to show composable ... when u use if and use a condition to show some different or multiple composables , the false part will be removed from the composition tree and even remember will be destroyed ... so whenever the condition turns true , the composable go for redraw and recreate not for recompose. At last u have to find another way to show and switch between views like navigations. or if you still want to use conditions ... you can save your states in a viewmodel .. Hope it helps😉
d
@Alex Vanyo yes, it is, thank you 😃
@Alireza Kherazani I was asking about
rememberSaveable
not just
remember
s
What Alireza mentions absolutely applies to rememberSaveable too. Give it a read again, you might understand what they're trying to convey.
a
@Dmitry Motyl That doesn't make any difference ... @Stylianos Gakis Correct , thank you so much 👍
d
Guys, the question was about
rememberSaveable
and what is missing for it to work. And @Alex Vanyo helped very well with it. But thank you for your explanations
a
@Dmitry Motyl That would be so great if you share that with us too plz
k
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) ) } } }
🧵 11
g
@Kiron James please, use thread