Anudeep Ananth
Anudeep Ananth

Reputation: 1123

Retain Scroll position of Recycler view when App is closed and opened

I am looking to implement saving the scroll position of my recycler view similar to how Twitter does it. So when a user scrolls down x no of items, closes the app, and then re-opens it then I want the scroll position to be maintained.

I am using the Paging 3 library with a Remote Mediator so all of the data is saved locally in a Room Database.

The adapter is hosted in a fragment and I have already tried the following

private var state: Parcelable? = null // initiating a parclable variable


    override fun onResume() {
      super.onResume();
      adapter.stateRestorationPolicy = 
      RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY
      binding.recyclerView.layoutManager!!.onRestoreInstanceState(state)
    }

    override fun onPause() {
      super.onPause()
      state = binding.recyclerView.layoutManager!!.onSaveInstanceState()
    }

I am using the latest version of the recycler view library

implementation 'androidx.recyclerview:recyclerview:1.2.0'

But this does not work.

Upvotes: 1

Views: 521

Answers (3)

Syed Ovais Akhtar
Syed Ovais Akhtar

Reputation: 21

You can use savedInstanceState for this by putting the last position into a bundle and on onResume method get saved position

for example:

onCreate(savedInstanceState:Bundle){

  savedInstanceState.put("yourKey",position)

  }

And when getting back

  onResume(savedInstanceState:Bundle){
   savedInstantState.get("yourKey")
  }

Upvotes: -1

ahmad bajwa
ahmad bajwa

Reputation: 1068

You can use SharedPreferences for this, when you are closing the app save the position in the preferences.

  SharedPreferences settings = getSharedPreferences("Position", MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("position", position);
    editor.apply();

After that you can get the position with it when you are re-opening the app.

 SharedPreferences settings = getSharedPreferences("Position", MODE_PRIVATE);
 int position= settings.getInt("position", null);
                    

Upvotes: 1

Amirsalar Salehi
Amirsalar Salehi

Reputation: 104

use recyclerViewId.getLayoutManager().findFirstVisibleItemPosition() to get the the first item index that showing at the screen. save it in Bundle in onSaveInstanceState() method when user stop the app(not kill) the position will be saved in Bundle and when start app get position saved in the Bundle in onRestoreInstanceState() method

for example

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(icicle);

  int position = recyclerViewId.getLayoutManager().findFirstVisibleItemPosition();

  saveInstance.putInt("recyclerViewLastPosition", position);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if(savedInstanceState != null) {
        int position = savedInstanceState.getInt("recyclerViewLastPosition", 0);
        recyclerViewId.scrollToPosition(position);
    }
}

Upvotes: 2

Related Questions