https://kotlinlang.org logo
Title
s

Se7eN

06/02/2021, 12:52 PM
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

Bryan Herbst

06/02/2021, 12:56 PM
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

Se7eN

06/02/2021, 12:59 PM
Makes sense. Thanks 🙂
h

Heikki Rauhala

06/09/2021, 2:07 PM
What about the alternatives that could be used similarly to
rememberSaveable
? I would like to implement an effect that would not run on back navigation.
a

Adam Powell

06/09/2021, 2:21 PM
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

Heikki Rauhala

06/10/2021, 5:52 AM
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

Se7eN

06/10/2021, 9:57 AM
Mine was to set a property based on a setting:
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?