Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 753
daniel m Avatar asked Nov 21 '12 18:11

daniel m


1 Answers

Both ScrollView and NestedSrollView has inner class extended from BaseSavedState which saves state of scrollPosition. You merely need to give your scrolling view an id to save and restore it's instance state automatically (you do NOT need to call it manually).. See Could I keep position of ScrollView after back to prev Fragment like ListView?

like image 176
Greg Ennis Avatar answered Nov 04 '22 09:11

Greg Ennis