2

I have some data loaded from API call in AsyncTaskLoader. Everything is good but when screen rotates the scroll position goes to first as the adapter is set again on config changes( But the API call is not made as it's a Loader). I tried saving scroll position as suggested in the following links

link 1

I saved the the scroll state in onSavedInstanceState() and restored it on onRestoreInstanceState() also tried in onPause() and onResume() but the recyclerview always shows items from first position when rotating the screen.

Here is code to save and restore the scroll state in my app

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelable(LIST_STATE, mMoviesListRecycler.getLayoutManager().onSaveInstanceState());


}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    listScroll=savedInstanceState.getParcelable(LIST_STATE);
   mMoviesListRecycler.getLayoutManager().onRestoreInstanceState(listScroll);
}
  • Turns out I have to wrap the **onRestoreInstanceState** in a **Handler** so as to delay the execution of restoring. I got this idea from [this](https://stackoverflow.com/questions/43113234/store-and-restore-recyclerview-position-not-working) post. It worked with this hack but i don't think this is a good idea. What you say? – Krishna Sapkota Dec 19 '17 at 11:03

0 Answers0