I found some code to save and restore the state of RecyclerView
after screen rotation, but it's not working.
Code:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(Constants.LIST_SAVED_POSITION, recyclerView.getLayoutManager().onSaveInstanceState());
}
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (savedInstanceState!=null && savedInstanceState.containsKey(Constants.LIST_SAVED_POSITION)){
Parcelable parcelable;
parcelable = savedInstanceState.getParcelable(Constants.LIST_SAVED_POSITION);
recyclerView.getLayoutManager().onRestoreInstanceState(parcelable);
}
}
The old position is not restored after a screen rotation. I know that I can save integer variables, but this way seems better.
Can someone help me solve this using this approach.