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(); } } 
  • declare a global static variable and save the state in it - Borisov Max
  • I just don't get it. I save layoutManagerState in onPause in a fragment variable, and in onResume layoutManagerState is already null, although in fact, variables should not be reset when you exit to the desktop via back - timuruktus
  • @BorisovMax, This is a very bad way. - post_zeew
  • one
    @timuruktus; Pressing the back button implies closing the application (if there is one activation on the stack, like you). I see no reason to save the state of RecyclerView in this situation. - post_zeew
  • @post_zeew The fact is that when opening another fragment and returning back, the position of RecyclerView is also not saved - timuruktus

1 answer 1

You can specify the launchMode attribute equal to singleTop in the manifest singleTop

singleTop us that an Activity can be placed on the stack several times. A new entry in the stack is created only if this Activity is not located at the top of the stack. If it is currently at the top, then the onNewIntent() method will work for it, but it will not be recreated.

If you need it only for BackStack , then you can pass the flag FLAG_ACTIVITY_SINGLE_TOP through the intent

  • Explain what the code does. What is layoutManagerState ? - eugeneek
  • Not copied, sorry - Borisov Max
  • There is no need to apologize. Just correct the answer and add explanations. - eugeneek
  • Corrected, now you fix;) - Borisov Max
  • Again, it is not clear how singleTop with singleTop will help to keep the RecyclerView state between application restarts. After all, the author of the question asks about it. - eugeneek