Regarding the Compose Pagination library, I know i...
# compose
v
Regarding the Compose Pagination library, I know it’s a little early to ask this since it literally just dropped (Thanks everyone for your hard work!), but is there a cleaner way to save scroll state and pagination data when using
LazyColumn
with
pager.flow.collectAsLazyPagingItems
? Right now in order to save both state and items I’ve been lifting out the
collectAsLazyPagingItems
output and storing it directly in the fragment since I can only call this method in a @composable and the queried list item data is nested into the PagePresenter.
For more info, I tried to “remember {}” the pagingItem but when I navigate to a new screen and back the data is cleared out (understandable since the composable instance reference is gone?) so I ended up just having a local variable in a fragment which does this
Copy code
if (pagingItems == null) {
    pagingItems = collectAsLazyPagingItems()
}
So that I have access to it when the fragment is returned off the backstack.
a
hey! the scroll position should be restored automatically. could you please file bug with more details and a sample code. thanks
👍 1
v
Oh the scroll position is restored properly, but the data that’s inside the
Pager.flow.collectAsLazyPagingItems
is not maintained across screens. I followed the same code as this CL https://android-review.googlesource.com/c/platform/frameworks/support/+/1428676/17/compose/paging/paging/samples/src/main/java/androidx/compose/paging/samples/PagingSample.kt#118
And I also just noticed that my method of restoring state by maintaining a reference to the LazyPagingItems<T> between screens stops the pagination from working after coming back… I’ll play with it some more before filing something
a
In general this example is oversimplified. I think we suggest to create the backend and the pager outside the composable as it should be a part of your business logic. try to move it to ViewModel at least
j
Does the pagination work both ways btw?
a
yes, it should
a
Yeah, the scroll state and the items simply isn’t being restored when navigating back into the Composable, evenwith the Pager being initialized within a ViewModel. I’ll try to replicate it in a sample project