Hello all, I have an activity which uses a ListView component. It's not a ListActivity, just a ListView and few other widgets within a LinearLayout. I need the activity to save/restore its state correctly on orientation change. Things mostly work fine, the only problem I encounter is that ListView doesn't save/restore its selection (selected item).
Currently I see following behaviour. When I change orientation from portrait to landscape it seems to be working (i.e. after rotation the selected item is the same as it was before rotation), but when I change it back (from landscape to portrait) - first item in the list is selected. I created simple descendant of the ListView with onSave/ RestoreInstanceState methods overridden, these methods just print selected ID/position to the log. Sample of the log I get. <changing orientation from portrait to landscape> onSaveInstanceState() - Position=3, ID=4 onRestoreInstanceState() - Position=0, ID=1 <call super.onRestoreInstanceState()> onRestoreInstanceState() - Position=0, ID=1 it seems that onRestoreInstanceState() doesn't work, but I see that proper item is selected (I guess selection is changed not at the same time but later, may be using View.post() or smth like this, so actual selection is set later than I get a value for printing). When I change orientation from landscape to portrait I get different log. <changing orientation from portrait to landscape> onSaveInstanceState() - Position=3, ID=4 onRestoreInstanceState() - Position=0, ID=1 <call super.onRestoreInstanceState()> onRestoreInstanceState() - Position=0, ID=1 onSaveInstanceState() - Position=0, ID=1 onRestoreInstanceState() - Position=0, ID=1 <call super.onRestoreInstanceState()> onRestoreInstanceState() - Position=0, ID=1 And now I see first item selected, not the one that was selected before rotation (it seems second call to onSaveInstanceState happens earlier than model is actually updated with correct selection value). There are few questions on this. 1. Why do we have onSave/Restore called twice when return from landscape to portrait (and only one on the way from portrait to landscape)? 2. Why list view can not restore its state? Is it because of some asynchronous calls that are made set position? 3. What is the right way to save/restore list view selection on rotation? I saw that Contacts application calls setSaveEnabled(false) and handles state on the activity level. Does it mean that there is no way to handle this at the widget level? I have few more questions, but would like to have these answers first and then elaborate others on. Thank you in advance. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---