how do I prevent the same `when (state)` to be run...
# compose
o
how do I prevent the same
when (state)
to be run on each recomposition ? cause I have the state changing from the viewmodel due to some request and something happening there that will cuase it to change, and then the when gets triggered again and the actions resulting from it happen again
e
I believe the technical term for that is a bug 😅 (jk) Jokes aside, you should add a conditional statement around what causes the state to change. Like, unless its an animation, its weird for a value to change on every recompostion
o
I see, you get what I mean though yea? Something like this
Copy code
Scaffold(
    topBar = {
        TopAppBarBack(
            title = stringResource(R.string.browse_events),
            onBack = { onNavigateBack() }
        )
    }
) {
    Column {
        FilterBar(filters)

        DividerFull()

        when (viewModel.locationState.collectAsState().value) {
            is LocationState.Disabled -> {
                Log.d("LocationStatus", "BrowseEvents: Location is disabled")
                onLocationDisabled()
            }
            is LocationState.NotSetOrNull -> {
                Log.d("LocationStatus", "BrowseEvents: Location is not set or null")
and then later after that, another state I check for as well, that other state would’ve changed by the time the code reaches it, so it recomposes, so the
when
above is also hit again
the fact that im using collectAsState for the first state thing should mean that the data inside is remembered
e
collect as state remembers. Are you sure
viewmodel.locationState
property doesnt change on every access?
o
does not change on every access…yyyup, the method in the viewmodel that changes it is only called once