0

So, I am currently supporting development efforts for an Android App that was previously developed by an entirely different group of developers.

As part of my ongoing support efforts, we had upgraded the TargetSdkVersion from 23 to 28, and accordingly, all the dependencies and support-libraries, and appcompat-libraries as well.

However, with the upgrade of recyclerview library, we have begun noticing a peculiar problem with the Infinite Scrolling enabled implementation for RecyclerView in the App.

Following is observed.

  1. Launch the App. Visit any Activity, Fragment or View that hosts a RecyclerView.

  2. Scroll-up. Tap on any card displayed in the RecyclerView. Will launch a new Activity or Fragment with details of info inside that tapped Card.

  3. Go back, close the existing Details Info view.

  4. In v28, the RecyclerView just comes back showing from the beginning of the scroll. The View does not auto-scroll or anything, it just presents itself with the very first item only.

  5. In v23, the RecyclerView retained the previous state, as in the set of Children that was displayed prior to tapping on a Card and launching a new Activity, Fragment or View, was what is displayed without any changes or auto-scrolls.

  6. The worst part, v28, nothing will retain the RecyclerView's state. Such as, invoking LayoutManager.onSaveInstanceState() in onPause(), storing that as a Parcelable and applying that in onResume() as LayoutManager.onRestoreInstanceState(Parcelable state) won't work. Otherwise, LayoutManager.findFirstVisibleItemPosition(), followed by calculating the firstVisibleItemPositionOffset in the onPause(), and using them both on LayoutManager.smoothScrollWithOffset in onResume(), none of the strategies seem to work.

Is GridLayoutManager broken and not retaining state in RecyclerView-v7:28.0.0?

What was the last version when RecyclerView retained it's state between activities and windows, and even orientation changes?

AndroidRocks
  • 292
  • 4
  • 16
  • 1
    As long as the RecyclerView has an `id`, it _should_ retain its state automatically. The only thing that would prevent this from happening is if the adapter doesn't have the correct data "in time" to restore the state. That is, if the adapter has to load the data from somewhere and the view state is restored before this data is loaded, the RecyclerView won't have the ability to restore the position correctly. Since you mentioned infinite scrolling, this seems somewhat likely. – Ben P. Nov 28 '18 at 23:13
  • Fabulous. This response gave me exactly what I should look at to fix the issue. Apparently, what's broken is a Fragment's LoaderCallback. The onLoadFinished method is invoked prior to onResume, only when returning from a different Activity or Fragment. SMH. Well, there was more logic to fetch more data when the LoaderCallback returned no elements apparently. But anyhow, I managed to find the troubled-spot and fix it. – AndroidRocks Nov 29 '18 at 19:27

0 Answers0