I know that LaunchedEffect(key)
is executed when composition starts, and is also cancelled and re-executed again when the key
changes. However, I want it to be executed only once and never again, I am using the following workaround but I feel like I am missing something:
if (!launchedBefore) {
LaunchedEffect(null) {
//stuff
launchedBefore = true
}
}
The code above works just fine. But it feels like a stretched workaround while something much simpler can be done. Am I misunderstanding how LaunchedEffect fully works?
I tried using null
and Unit
as keys cuz they never changed, but the code is executed every time a composition takes place.