Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Why list always start from starting?

Hello Ben Deitch Sir, I just completed the Android Application Lifecycle course and its awesome I learn a lot about SharedPreferences but I have a question.

When we scroll down in the list and click the home button and opens app again list position stays same but when I'm in last of the list and click the back button and open the app again list position start from the beginning. How to save the position of list, any link to tutorial is appreciated :)

1 Answer

Rares Conea
PLUS
Rares Conea
Courses Plus Student 15,000 Points
  1. Declare at the top of your class a Parcelable state;
  2. In onPause save the state of your ListView/RecyclerView(name you gave to the object which you used to find the view by id): state = <how you named your <ListView/RecyclerView>.onSaveInstanceState();
  3. In onViewCreated after setting the adapter for the listView/adapterView: if(state != null) { <listView/recyclerView>.onRestoreInstanceState(state); }