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

Vincent tiensi

11/05/2020, 9:26 PM
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

Andrey Kulikov

11/05/2020, 10:00 PM
hey! the scroll position should be restored automatically. could you please file bug with more details and a sample code. thanks
👍 1
v

Vincent tiensi

11/06/2020, 1:02 AM
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

Andrey Kulikov

11/06/2020, 11:10 AM
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

Joost Klitsie

11/06/2020, 11:39 AM
Does the pagination work both ways btw?
a

Andrey Kulikov

11/06/2020, 11:50 AM
yes, it should
a

allan.conda

11/10/2020, 8:30 AM
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
26 Views