android / codelab-android-paging

Jetpack Paging codelab

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

List blinking when I restore state in fragment

artemkamko opened this issue · comments

I have two fragments and bottom navigation. Every fragment contain list with paging. I use RemoteMediator to get data from api.
If I use launch in fragment then I always recreate list if I navigate between first and second fragment.

lifecycleScope.launch {
            viewModel.items.collectLatest {
                adapter?.submitData(it)
            }
        }

If I use launchWhenCreated or launchWhenStarted then list save it state, but when I navigate between first and second fragment it show blank list for maybe 1 second and it's create visual blinking.
How can I save paging 3 list state and show list immediately on fragment created?

It's also behave the save way as launchWhenCreated or launchWhenStarted if I use something like CoroutineScope(Dispatchers.IO).launch or any other dispatcher

@artemkamko I believe you want to use the cacheIn method. You will need to use this with a coroutineScope that is not affected by the onPause and onResume Fragment lifecycles, so most likely a viewModelScope.

If you are still experiencing issues, please file a bug on the paging issue tracker, as github issues are specifically for the codelab.

@tunjid Thanks for the answer. I did use cacheIn method in my viewModel where I store flow, but it's not help.
Ok I'll post the issue on the issue tracker

@artemkamko hi again, I took another look and you're right. The issue seems to be because even through the remote mediator is not loading, it doesn't mean that the pager has fetched data from the db. So the list is made visible, the old list is shown, diff util calculates the change, removes the old items, then puts the new items in.

I'll try experimenting with a debounce on the adapter.loadStateFlow instead of adapter.addLoadStateListener to see if there's anything to help mitigate this, and also check for more simpler ways.

I reopened the issue as its very easy to repro with the codelab, so I'll keep updating you as I find out more details.

@tunjid Ok, thanks

@tunjid Hi, do you have any progress in this issue?

Hi @tunjid and @artemkamko, any news about this? I am experiencing (maybe) the same issue.

I have a bottom navigation view and two fragments in the navigation, both contain shared items so if, in the first fragment, I updated an item and then changed the fragment to the second one the list blinks. If I don't update the item the blink is not produced. Think that when the pagingSourceFactory invalidates is when the blink is produced.