daniel m daniel m - 1 year ago 151
Android Question

ScrollView in Fragment retain position

I have 2 Fragments A and B.

A contains a ScrollView, if I scroll down and press a button Fragment A gets replaced by Fragment B. Now i press the back-button and Fragment A comes back to the screen, but the ScrollView is not at the same position as it was when I switched to Fragment B.

But when I do the same with a Fragment (not a ListFragment!) that contains a ListView the scroll position is maintained.

Is there anything special I have to do with my ScrollView?!

Greetings, Daniel.

Answer Source

You have to implement onSaveInstanceState(Bundle) in your Fragment to save the data (list scroll position), which you can then access from onCreate(Bundle), onCreateView(LayoutInflater, ViewGroup, Bundle) or onActivityCreated(Bundle).

Edit: According to this answer, you only need to call setRetainInstance(true) in your Fragment.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download