10

Possible Duplicate:
Maintain/Save/Restore scroll position when returning to a ListView

How can I maintain the position of my ListView in my activity when I go to another activity (by launching another intent) and then come back (press the back button)?

Thank you.

Community
  • 1
  • 1
michael
  • 93,094
  • 111
  • 230
  • 334

5 Answers5

28

Declare global variables:

int index = 0;
ListView list;

and make a reference to your ListView in onCreate():

list = (ListView) findViewById(R.id.my_list);

Next, in onResume(), add this line at the end:

list.setSelectionFromTop(index, 0);

Lastly, in onPause, add the following line to the end:

index = list.getFirstVisiblePosition();
Phil
  • 34,061
  • 21
  • 117
  • 154
  • Furthermore, I would save the index in a sharedPreferences object so that when the application goes out of scope, it still retains the index – mray190 Jun 17 '15 at 13:59
  • 1
    @mray190 that is what saving the instance state is for. You can save this value in a bundle on orientation changes then restore the scrolled state when the view is recreated, at an Activity or Fragment level. – Phil Jun 17 '15 at 14:02
7

Do simple....

@Override
protected void onPause()
{
   index = listView.getFirstVisiblePosition();
   // store index using shared preferences   
}

and..

@Override
public void onResume() {
super.onResume();
// get index from shared preferences

if(listView != null){
    if(listView.getCount() > index)
        listView.setSelectionFromTop(index, 0);
    else
        listView.setSelectionFromTop(0, 0);
}
Fredrik Ljung
  • 1,405
  • 11
  • 26
Rajeev Mathur
  • 71
  • 1
  • 2
2

Please note that using ListView.getScrollY() DOES NOT WORK WELL for restoring the scroll position.

See Android: ListView.getScrollY() - does it work?

It is referring to the scroll amount of the entire view, so it will almost always be 0.

It happened to me too most of the time that this value was 0. ListView.getFirstVisiblePosition() with ListView.setSelection() works more reliably.

Community
  • 1
  • 1
Martin Schmidt
  • 1,231
  • 10
  • 5
2

You should use onSaveInstanceState to store the scroll position and then use either onCreate or onRestoreInstanceState to restore it.

http://developer.android.com...#onSaveInstanceState...

clahey
  • 4,662
  • 3
  • 25
  • 19
  • I tried your way. mListView.getSelectedItemPosition(); return me a -1 and mListView.getScrollY() give me a 0. I am clearly scroll down to my ListView when I do my test. Any idea why it is not working? – michael Apr 28 '10 at 23:26
  • What did you do to try my way? – clahey Apr 29 '10 at 00:20
  • I did: protected void onSaveInstanceState (Bundle outState) { super.onSaveInstanceState(outState); int scroll = mListView.getScrollY(); System.out.println (" scrollY" + scroll); } – michael Apr 29 '10 at 00:27
  • Ah, I have no idea. I haven't used a listView yet. I'm just starting on android. – clahey Apr 29 '10 at 04:07
  • Does it work when called from onPause? – clahey Apr 29 '10 at 04:08
  • No. It also gives me 0 when I call mListView.getScrollY(). – michael Apr 29 '10 at 04:51
  • That is the wrong method. You should have been storing myListView.getFirstVisiblePosition() in onPause(), then restoring that position with myListView.setSelection(). Where you store that int is up to you depending on whether you want it to persist after destroy – HXCaine May 23 '10 at 01:10
1
@Override
protected void onPause()
{
    // Save scroll position
    SharedPreferences preferences = context.getSharedPreferences("SCROLL", 0);
    SharedPreferences.Editor editor = preferences.edit();
    int scroll = mListView.getScrollY();
    editor.put("ScrollValue", scroll);
    editor.commit();
}

@Override
protected void onResume()
{
    // Get the scroll position
    SharedPreferences preferences = context.getSharedPreferences("SCROLL", 0);
    int scroll = preferences.getInt("ScrollView", 0);
    mListView.scrollTo(0, scroll);
}
CaseyB
  • 24,194
  • 11
  • 70
  • 106
  • If I use 'getSharedPreferences', does it mean it will persist to the disk? i.e. it will preserve the value after I reboot the phone? What if I don't want that? when I reboot the phone, it will start from 0? – michael Apr 27 '10 at 21:38
  • It will persist beyond shutdown. If you do not want that then you can write the value to a member variable and hope that it persists. It should if the app doesn't get garbage collected. You just need to decide which side you'd like to err on. – CaseyB Apr 28 '10 at 01:09
  • 1
    I think line 6 in the onPause should read editor.putInt("ScrollValue", scroll); – Arunabh Das Sep 06 '11 at 20:48
  • 1
    mListView.getScrollY() returns 0 , always. read here: http://stackoverflow.com/questions/2132370/android-listview-getscrolly-does-it-work – android developer Apr 28 '13 at 23:02