Hey everyone, I’m facing an issue in my Compose Mu...
# compose
v
Hey everyone, I’m facing an issue in my Compose Multi-platform app, with screens being recreated when navigating back I’m using Voyager for Navigation When I navigate back • The Lazy Column Scroll State is lost (I’m using
rememberLazyListState()
• ViewPager State is lost (I’m using
rememberPagerState()
• All Launched Effects of all Composables run again (
LaunchedEffect(Unit)
) • Compose States reset to their default value for a small amount of time, then get back to original value As of now, I’ve been using a workaround, i.e. to store lazyListState, viewPager State in ViewModel, which I dont think is right Does anyone know, if this is the default behaviour of Compose, or its Voyager’s Navigation issue? Cc: @लातों वाला भूत
a
Compose itself doesn't do anything with preserving the state between screens. It's a responsibility for navigation. Usually navigation libraries use SaveableStateHolder, and I remember Voyager used it as well, but I might be wrong.
So, you might be using Voyager incorrectly, or maybe there is a bug in the library. Worth checking with docs.
v
@Arkadii Ivanov thanks Does this kind of behaviour normally happen in AndroidX Navigation Compose?
a
I'm pretty sure navigation-compose preserves the UI state, as well as all other navigation libraries.
s
@Arkadii Ivanov we use Pre-compose library that also that similar behaviour,
a
What I'm saying is that it's by design that all
remember
-ed values are lost, as a screen Composable exits the composition when another screen is pushed to the stack. But the screen Composable should correctly restore its state, r.g. LazyColumn is able to keep its scroll position. Make sure you keep your screen data in a ViewModel (or whatever concept is provided by the navigation library).
v
@Arkadii Ivanov All my data is in viewmodel and is restored again Its just the ui related states, like fab expansion state, scroll state, viewpager state, all is lost All remembered states get reset
a
This is by design I believe, rememberSaveable should do the trick.
v
@Arkadii Ivanov does that work in Compose Multiplatform, targeting Android and iOS only?
a
I think yes, but the UI state is only preserved while the process is alive.
Decompose is able to preserve the state over process death, but only for business logic (aka ViewModel). Not sure about other libraries.
v
I'm considering shifting to Decompose, when navigating back to a screen does it preserve the ui states, or is it the same as what I'm facing with Voyager?
a
I think all navigation libraries are kinda the same in this regard. All
remember
-ed state is lost when another screen is pushed to the stack, all `LaunchedEffect`s are terminated, etc. But
rememberSaveable
-ed state should be restored correctly (as long as there as no process death involved).