One option would be to save the index and the position of the first item in the list before going to the layout of details, this is not finalizing the items screen . The reason you're losing position, maybe because% of the% of the items screen is re-browsing the list (check this). Here's what it would look like:
int index = listview.getFirstVisiblePosition();
View v = listview.getChildAt(0);
int itemTop = (v == null) ? 0 : (v.getTop() - listview.getPaddingTop());
To restore, you can use the onResume
method on your setSelectionFromTop
:
listview.setSelectionFromTop(index, itemTop);
See this answer in SOen.
Another option would be to save the state using onResume()
. By exploring the activity lifecycle, you use Parcelable
so that by the time you quit the activity, save the state using onPause
list relation. Here's an example:
Parcelable state;
@Override
public void onPause() {
state = listview.onSaveInstanceState();
super.onPause();
}
To restore, first check whether there is a saved state, for an example, if a value of onSaveInstanceState
has been assigned a value. Soon after, use the method state
passing as parameter the state. See:
if(state != null) {
listview.onRestoreInstanceState(state);
}