Chuck Stein
08/18/2022, 2:18 AMrememberSaveable
with inputs
but despite what the javadoc says, the init
block is not rerun when the inputs
change. I need the value to be reinitialized when we return to this screen by popping the backstack and giving it new inputs
. Is this not possible?Ian Lake
08/18/2022, 3:36 AMIan Lake
08/18/2022, 3:45 AMChuck Stein
08/18/2022, 4:02 AMrememberSaveable
or anything else) to remember a value across config changes or process death but not when we return to the screen by popping the backstack?Ian Lake
08/18/2022, 4:03 AMIan Lake
08/18/2022, 4:13 AMChuck Stein
08/18/2022, 4:42 AMremember { LazyListState(firstNewItemIndex) }
. I can't use rememberLazyListState(firstNewItemIndex)
because that uses rememberSaveable
under the hood, meaning we can't recreate the LazyListState
when we return to screen A with a different firstNewItemIndex
. But now I realize this makes the scroll position less persistent than using rememberLazyListState
. So is there any way to achieve my goal of auto-scrolling to the first new item in the list, without losing the scroll position on config changes or process death? Originally I was thinking a I could call lazyListState.scrollToItem(firstVisibleItemIndex)
inside of a LaunchedEffect
, but then that LaunchedEffect
would be called again after a config change or process recreation, rather than maintaining the scroll position the list was in just before the config change / process death.Ian Lake
08/18/2022, 4:47 AMChuck Stein
08/18/2022, 5:43 AMfirstNewItemIndex
. It's the "you'd only do the scroll to if there is a result to handle" part that I must be doing wrong. This result is part of the saved state handle, so that saved state persists across config changes / process death. If we return to Screen A from Screen B with a firstNewItemIndex
result, then auto-scroll to this item via LaunchedEffect
, then the user manually scrolls to a new position, then there's a config change, we would then want to maintain that new position that the user scrolled to, but since LaunchedEffect
will be re-entering the composition, it will instead auto-scroll to firstNewItemIndex
again.
I suppose I could set the firstNewItemIndex
value in the `navBackStackEntry`'s saved state handle to null after I retrieve it from the saved state handle for the first time, but in practice when I tried that originally, it seemed that the code that pulled the result from the saved state handle then nullified it was getting called multiple times every time we navigate back to Screen A, so after the first time the code is called and nullified the firstNewItemIndex
, it would compose Screen A with a null position to scroll to, so the auto-scroll wouldn't work.
Through rubber ducking I'm now realizing the code was probably called multiple times because upon navigation back to that destination composable, the destination might recompose more than once before settling. So the solution is probably nullifying the result in the saved state handle only after we perform the auto-scroll inside LaunchedEffect
, via callbacks leading all the way back to the composable destination where we have access to the `navBackStackEntry`'s saved state handle.Ian Lake
08/18/2022, 6:04 AMsavedStateHandle.remove(YOUR_RESULT_KEY)
after you're done processing it e.g. at the end of your LaunchedEffect
block after the scroll to call