What causes the ListView to retain its scroll position after changing orientation?

I have an Activity containing a ListView. I populate the list using the SimpleCursorAdapter. When I launch the application, I can manually scroll down to the ListView (for example, to the 10th item). Then, when I rotate my phone, it destroys and re-creates my activity, and the ListView is recreated as expected.

But on my phone after the rotation, the newly created activity shows my ListView still in my manual scroll position (in this example, the 10th row). I'm sure my code does not call ListView.setSelection (int), so what causes this to reposition ListView?

It may seem pretty convenient, but that’s not what I want. Is this part of ListViews behavior? If so, how to stop / redefine this re-positioning?

+4
source share
3 answers

Read again on the topic ....

View status is saved automatically (including my ListView). This is due to the default implementation of onSaveInstanceState () / onRestoreInstanceState (). Using the debugger, I see that savedInstanceState contains an entry from my ListView - this is what automatically repositions it. To stop this from happening, I can simply override onSaveInstanceState ().

+4
source

Have you used ListActivity as a base class for your activity? If so, then this is most likely a function, since it overrides onRestoreInstanceState. I would try to inherit from a simple Activity.

0
source

ListView restores its state (including the scroll position) after the screen is rotated in the same way and for the same reason as, for example, EditText saves the text you entered in it. This is how the Android platform is designed. You can stop this save / restore behavior by setting the android:saveEnabled="false" attribute for the ListView in the xml layout file. Or you can call the setSaveEnabled(false) method on a ListView for the same purpose.

0
source

All Articles