This is very trivial and am sure i am missing something basic here
i have a activity with a listview (populated thru ArrayAdapter
)
in the adapter I have implemented my onClick listener that launches another activity.
Based on a few suggestion i have save the state of the getSelectedItemPosition
in the onSaveInstanceState
which i have overridden
The problem is when i am navigate back to the mainactivity, i am expecting onRestoreInstanceState
to be called where i can set the setPosition to the position I have stored in onSaveInstanceState
, but its not called.
So the end results is that when i can come back to the mainactivity the selection is rolled to the first row. which is not a very good user experience.
You can achieve this by saving the lastItemSelected position
in a int field of your Activity called lastItemSelected
(remember to update this variabe every time you click on a element of the list)
Save last item
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
lastItemSelected = position;
}
});
Then on the onResume()
lifecycle method do:
yourListView.setSelection(lastItemSelected)
onSaveInstanceState
and onRestoreInstance
etc etc are method used for restore your activity state when it is forcefully terminated by the OS.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.