1

I'm doing this and is not working, I have researched all over the place but I can't find the right solution. I have researched about onSaveInstanceState() but I can't get it to work too.

My problem is that when I scroll and press an item from the listview it opens ok, but when I go back it goes to the top of the listview instead of showing me the last item I was on.

I first declare an int position:

int position;

After that I get the position from my listview in my onCreate:

position = mListView.getFirstVisiblePosition();

And after setting the adapter I'm setting the selection to the position of the listview:

mListView.setAdapter(mAdapter);
mListView.setSelection(position);
halfer
  • 18,701
  • 13
  • 79
  • 158
Gastón Saillén
  • 9,076
  • 4
  • 39
  • 58
  • Take a look at this https://stackoverflow.com/questions/3014089/maintain-save-restore-scroll-position-when-returning-to-a-listview – Umair Jan 30 '18 at 13:32

2 Answers2

1

When you click on Listview item of activity A, for Ex 5th position of the listview, now what I recommend is that you should store this value in the Preference and move to Activity B and when you return from Activity B to Activity A than just simply retrieve the position from the preference in onResume() method and just call the yourListView.smoothScrollToPosition(YOUR_POSITION);

Pranay Soni
  • 375
  • 2
  • 12
1

First override onSaveInstanceState() and save the scroll position. Then override onRestoreInstanceState() to load the scroll position and scroll the view to it.

Note that this will only maintain the scroll position while your app is running. If you want to restore the position when your app is closed and restarted, then you need to use SharedPreference. You should override onPause() to save the position in SharedPreference then override onResume() to load the position.

Code-Apprentice
  • 69,701
  • 17
  • 115
  • 226