One Off the Slack: Saving Multiple States for a List

Rin Orz asked:

I am writing a file manager, when I click on the folder, the LazyColumn items data will be reloaded to enter the folder, then how can I remember the list position of the previous page so that I can automatically display it to the correct one when I return next time position, for example, the path a/b/c, when go to c, I should save the location of a and b

[to navigate between pages, I] just replace the data through the viewModel to reorganize the LazyColumn, probably a fake navigation implementation

Google’s Ian Lake suggested:

It sounds like, if you’re using the same LazyColumn, then you’ll need to hoist and save the LazyListState for each of your lists (i.e., one for a, one for b, etc.), just passing down the right state with the right contents of the list as you swap between them

Rin was concerned about performance:

If there are too many file paths (pages), will holding a LazyListState for each lists cause performance problems? Maybe just save its initialFirstVisibleItemIndex and initialFirstVisibleItemScrollOffset for each lists?

Ian indicated that this was not necessary:

The LazyListState itself is very small; you’d want to hoist the whole object


Read the original thread in the kotlinlang Slack workspace. Not a member? Join that Slack workspace here!