```var lselected by mutableStateOf(true) ``` But ...
# compose
m
Copy code
var lselected by mutableStateOf(true)
But again if it's a checkbox it's fine ...You can see the issue by clicking on settings languages +languages
y
Shouldn't this be
var lselected by remember { mutableStateOf(true) }
?
Don't forget the import
Copy code
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
m
So sorry but apparently this is not the point ... thanks anyway
t
it makes a big difference and should solve your problem. Because when it is not wrapped by remember the initial value true is set every time again when this component is rendered. This could lead to this oscillation your observed.
m
and then how can you explain that it works perfectly with checkbox?
đŸ¤” 1
t
Did you tried with remember?
m
You really don't need remember at all, in fact using it or not it makes no difference , someone answered that there's a bug and you should write
Copy code
onCheckedChange = { lselected = it}
and then it works. Still I see your point regarding remember, but in practice it is still from me unclear where I shall absolutely use it or not (remember), so my rule of thumb is right know use mutablestateof and if doesn't work try remember. And well that's all I can remember ....but slowly but constantly thinks are being settled and compose has very very much improved over time
y
I believe in your case it just works because your tree doesn't get recomposed above the declaration of the state, so it might work, but shouldn't be left that way anyway, as it is error prone. A simple analogy would by creating objects inside
onDraw
call, you'd re-create the objects each time the view is drawn, this is not what you want here.
t
I thought that i understood it but maybe not. And before state {} was removed it was the same like
val v by remember { mutableStateOf(..) }
For me the rule of thumb is when you declaring mutableState inside of a composable function you should use remember. But maybe someone could correct me if this is a wrong assumption. If you not doing this you could get some wired side effects.
m
I think that it's a matter of maturity (of the framework not our) , we all would like to know rocked science what's right and what's not , I take a very pragmatic approach to it without making to much assumptions, I too believed that I needed remember, but the reality proved not always .... I appreciate your effort to settle this issue for once and ever. And I can't help to tell you but I love cats mine is black .
t
@MBegemot
really don't need remember at all, in fact using it or not it makes no
difference , someone answered that  there's a bug and you should write
onCheckedChange = { lselected = it}
It is not a bug in Checkbox. This is the correct behavior. Because in compose the components do not have a state. And the onCheckedChange callback just tell you that the user wants a new state. So you need to forward this state to your own itnernal state. In your case it is the lselected variable.
Maybe there is a bug in Switch when it does this thing differently there