https://kotlinlang.org logo
#compose
Title
# compose
o

oday

07/02/2022, 6:25 PM
so i have a pretty basic misunderstanding towards how state is used inside of an Activity, I have the following:
Copy code
class MainActivity @Inject constructor() : ComponentActivity() {

    private val viewModel: MainViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            if (viewModel.uiState.collectAsState().value.authenticated) {
                Home()
            } else {
                Authentication()
            }
        }
    }
}
this
authenticated
field is going to change to
true
or
false
inside the viewModel depending if the user is authenticated or not the above code will first come up with false for that field, showing the Authentication composable and then the change will happen triggering the recomposition and then it will come up with true and show the Home composable how do I
listen
to changes for that field, is the above even correct or how things should be done?
z

Zach Klippenstein (he/him) [MOD]

07/05/2022, 4:29 PM
either
authenticated
needs to be backed by a
MutableState
, or whatever holds
authenticated
(the type of
uiState
) should be immutable and the entire object updated when the authentication status changes.
1
2 Views