3

I have an issue, similar to the one described here. I have a master and detail flow, where the first has a list of items, obtained through Pager, using RemoteMediator. When navigating to the detail screen and back, the scroll state is initially restored correctly, however the LiveData<PagingData> is emitted several times, which causes the recyclerView animation to trigger and scroll slightly up. The state is properly restored if I'm near the top of the list, but it scrolls further the further down the list I was.

The pagingData is observed as liveData from activity like so in onCreate:

viewModel.items.observe(this) { pagingData ->
    adapter.submitData(lifecycle, pagingData)
}

I assume this happens due to pagingData being emitted multiple times and based on my debugging, inserting the same pages again after coming back to this activity. How do I fix this?

user_4685247
  • 2,878
  • 2
  • 17
  • 43
  • try using `viewLifecycleOwner` instead of `this` and `viewLifecycleOwner.lifecycle` instead of `lifecycle` , it worked for me [ref](https://github.com/sarafanshul/Naruto/blob/c4a3f5ae6cb75c64c12311b5d098a19ebc855cf7/app/src/main/java/com/projectdelta/naruto/ui/main/character/list/CharacterListFragment.kt#L83) – Anshul Nov 26 '21 at 16:26
  • there's no such thing as `viewLifecycleOwner` in an activity. – user_4685247 Nov 29 '21 at 06:11

0 Answers0