Issue
When I use LaunchedEffect
in jetpack compose screen, what is difference between using viewmodel and Unit
in key1 parameter?
Solution
From the documentation (href="https://developer.android.com/jetpack/compose/side-effects#launchedeffect" rel="nofollow noreferrer">https://developer.android.com/jetpack/compose/side-effects#launchedeffect) :
LaunchedEffect
restarts when one of the key parameters changes.
Using Unit
would guarantee this will only run once for the composable scope (Unit
being an object) no matter how many times it is recomposed, where as if the viewmodel "changes" (equals method returns false) it would be "launched" again (lambda block would execute again, cancelling the previous suspend block first).
Answered By - Mark
Answer Checked By - Clifford M. (JavaFixing Volunteer)