FirebaseUI-Android: FirebaseRecyclerAdapter and RecyclerView scroll position is lost on orientation change

Environment

  • Android device: API 27 Emulator / Google Pixel
  • Android OS version: 27 / 8.0.0
  • Google Play Services version: whatever the emulator has / 11.7.45 (940-174122868)
  • Firebase/Play Services SDK version: 11.4.2
  • FirebaseUI version: 3.1.0

The problem:

On orientation change the recycler view using FirebaseRecyclerAdapter looses it’s scroll position and is always scrolled to the top.

Steps to reproduce:

  1. In the https://github.com/firebase/FirebaseUI-Android/blob/master/app/src/main/java/com/firebase/uidemo/database/realtime/RealtimeDbChatActivity.java#L110 comment out:
        // Scroll to bottom on new messages
        adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onItemRangeInserted(int positionStart, int itemCount) {
                mRecyclerView.smoothScrollToPosition(adapter.getItemCount());
            }
        });

so it does not get execute. This is needed so there is no extra logic forcing a scroll position. 2. Build the demo app 3. Add a bunch of chat messages, a lot so you get a scrolling regardless of orientation 4. Scroll neither to the top nor the bottom 5. Rotate the emulator

Observed Results:

  • After rotation the recycler view lost it’s scroll position and is scrolled to the top.

Expected Results:

  • The recycler view should keep it’s scroll position on orientation change.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (1 by maintainers)

Most upvoted comments

@SUPERCILEX I solved my problem with a simpler solution. I just called adapter.startListening(); in onViewCreated() instead of onStart and called adapter.stopListening(); in onDestroyView() instead of onStop() That prevented the entire list from regenerating while coming back from next activity and thus retained the scroll position where it was previously.

@SUPERCILEX @ncherian hi im new to android development… how can i get it done? can you give me detailed instruction on how can i get it done it?

Thanks everyone for commenting! This seems like it’s working as intended from the FBUI perspective, so closing the issue.

I also experience a similar issue. The scroll position is not retained when returning back from next screen and the screen goes back to first item on recycler view list. How to solve it?