At the moment, the application saves the state of RecyclerView when you exit to the desktop via the Home key. However, if I go to another fragment, or go to the desktop with the Back button, and go back to the fragment, the state of RecyclerView will not be saved!
How to save now:
@Nullable @BindView(R.id.feedList) RecyclerView recyclerView; @Override public void onSaveInstanceState(Bundle outState){ super.onSaveInstanceState(outState); //If we catch an exception, it means that recyclerView is not yet created. try{ outState.putParcelable(RECYCLER_VIEW_STATE, recyclerView.getLayoutManager().onSaveInstanceState()); } catch(Exception ex){ ex.printStackTrace(); } } @Override public void onViewStateRestored(@Nullable Bundle savedInstanceState){ super.onViewStateRestored(savedInstanceState); //If we catch an exception, it means that recyclerView is not yet created. try{ layoutManagerState = savedInstanceState.getParcelable(RECYCLER_VIEW_STATE); } catch(Exception ex){ ex.printStackTrace(); } } @Override public void onResume(){ super.onResume(); if(layoutManagerState != null && recyclerView != null && recyclerView.getLayoutManager() != null){ recyclerView.getLayoutManager().onRestoreInstanceState(layoutManagerState); } } @Override public void onPause(){ super.onPause(); if(layoutManagerState != null && recyclerView != null){ layoutManagerState = recyclerView.getLayoutManager().onSaveInstanceState(); } }
RecyclerViewin this situation. - post_zeew