Is `LaunchedEffect` supposed to relaunch on config...
# compose
s
Is
LaunchedEffect
supposed to relaunch on configuration change? If yes, is there something else I can use that doesn't relaunch on config changes?
b
If you declare
android:configChanges="[…]"
in your manifest then
LaunchedEffect
should not get re-triggered. Otherwise your entire Activity will be recreated on configuration changes so
LaunchedEffect
getting re-triggered is expected.
s
Makes sense. Thanks 🙂
a
Generally speaking effects should be used to implement declarative ideas, which implies the kind of idempotence where running on back navigation should be correct and desirable rather than something to be avoided. Can you give an example of what you're trying to do?
h
I've been working on LazyListState, and now that I look at it the next day I just ended up having proper
inputs
for the saveable state and did not need to use an effect at all.
It's just that I needed to write a version of
androidx.compose.foundation.lazy.LazyListStateKt#rememberLazyListState
that takes
inputs
and passes them to the
rememberSaveable
.
s
Mine was to set a property based on a setting:
Copy code
LaunchedEffect(isDarkTheme) {
    val darkBG = (darkBackgroundFlow(context).firstOrNull() ?: false) || isDarkTheme
    elementsManager.defaultTextColor = if (darkBG) onDarkBackground else onLightBackground
}
Since I'm also changing the
defaultTextColor
later based on the last color selected by the user, rotating the screen resets it to the
onDarkBackground
or
onLightBackground
, which I didn't want. But, now that I think, passing
darkBG
to my view model (where I'm creating the
elementsManager
) would be a better option, right?