0

im kinda new to android and i am following a the google dev blogs, and tutorials on Lynda.com and i encountered an issue which i can't seem to find a solution for.

I have a ListView full of items , and on click it will load a new Activity. all of that works as expected. But when i click back on the new activity the parent loads up as if it were entirely new. back at the top of the list view.

Lets say i scroll down to the middle somewhere. I click an item, i get the results whatever, I go back to parent and then i am back at list item (0) again.

See image: http://imgur.com/CuNuQe0

how would i go about it keeping its position on back press?

thanks

173901
  • 669
  • 1
  • 6
  • 24
  • Take a look: [Maintain/Save/Restore scroll position when returning to a ListView](http://stackoverflow.com/questions/3014089/maintain-save-restore-scroll-position-when-returning-to-a-listview) – gersonmdesouza Jun 30 '15 at 13:38
  • Try something like [THIS ANSWER](http://stackoverflow.com/a/11187909/1289716) – MAC Jun 30 '15 at 13:39

2 Answers2

1

You can help your self with this thread: Programmatically scroll to a specific position in an Android ListView

You should remember the position at which you where when you selected the element. And when you go back, you should set that position with:

getListView().setSelection(21);
Community
  • 1
  • 1
Jure
  • 760
  • 6
  • 23
1

I had the same problem, and found the solution here
The idea is to save the state of your listView when you exit the activity and then restore it :

Parcelable state;

@Override
public void onPause(){
    state = yourListView.onSaveInstanceState();
    super.onPause();
}

@Override
public void onResume(){
    //reload data if needed
    if(state != null) {
        yourListView.onRestoreInstanceState(state);
        state = null;
    }
    super.onResume();
}

Hope it helps !

Community
  • 1
  • 1
Bastien Viatge
  • 195
  • 1
  • 15