https://kotlinlang.org logo
#compose
Title
# compose
p

Pardip

10/31/2023, 8:53 AM
I’m looking for material design 3 navigation behaviour sample example to achieve specially following two point 1. Preserve state: If the user previously interacted with this destination, it returns to that place on the screen, including scroll position, tab selection, 2. Reset state: Any prior user interactions and temporary screen states are reset, including scroll position, tab selection, How can I achieve above point in compose using MD3 navigation, any sample code, idea would be great to me. Thanks
d

Dhaval Gondaliya

11/02/2023, 5:58 AM
You should try to use ViewModel's savedInstancestate for saving all these state.
c

clark

11/02/2023, 9:04 PM
If you use
rememberSaveable
(which rememberLazyListState uses under the hood) your state should be saved into NavigationBackStackEntry’s SavedStateHandle. Combining that with the
saveState
on NavOptions
popUpTo
builder and the
restoreState
setting on NavOptions, you should be able to achieve your goal
One thing that can trip you up in restoring list state is making sure that when you navigate back to that screen, the list has the same content as before. For example, imagine you have a screen with 100 items in lazy list and your scroll position is at around the 50th item. If you leave the screen and come back and your view model has to re-load the 100 items and displays a temporary loading state, the scroll position will be changed to something within the bounds of the new list size. So make sure your view model/list contents are also being restored. (Hilt should handle this automatically in most cases)
If you use Anvil, it will depend on how you set up your view model creation/injection
p

Pardip

11/06/2023, 8:05 AM
Thanks for your input. I found a way to deal with it.
👍 1