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
答案 0 :(得分: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);
答案 1 :(得分: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 !