Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a RecyclerView remember its position when being recreated after poping the backstack?

I have a Fragment, that I replace with a transaction with a new one

That Fragment contains a RecyclerView with a big number of items.

When the user presses the finish button on the new fragment I pop the backstack, and the old fragment is recreated.

This causes the OnCreateView method to be called again and everything works fine with that.

My problem is that the RecyclerView in it is recreated, the adapter is reset, but it seems to automatically move to the position that it left (which I assumed would only happen if I used transaction add not transaction replace)

As a side note the adapter still calls OnBindViewHolder and OnCreateViewHolder for the visible items.

My question is , why does this happen? since the whole fragment is recreated shouldn't it reset to the first position? Other than manually calling scrollToPosition(0) before loading the new fragment, is there another way to make the fragment not remember anything and be actually fully recreated when returning ?

like image 201
Cruces Avatar asked Jun 20 '18 08:06

Cruces


1 Answers

The LinearLayoutManager stores the scroll position in onSaveInstanceState. Since the SavedState parcelable is stored with the view id, as long as the position is not out of bounds while returning from the backstack, the scroll position is restored.

like image 182
user3648939 Avatar answered Oct 23 '22 09:10

user3648939