-1

I have a list of strings, let's say:

l = [a, b, c, d, e, f];

with 6 elements. After working with first 3 elements, for example (a, b, c), I close down my app. I again open my app and want to start my work from 4th element i.e. 'd'. How do I achieve this in Android? At present whenever I close my app, the list gets reset to first element. Again, in my case, the list is really long and when it is used by different users, I don't know a priori at what element a random user will close the app.

I want a method which can keep track of the last element being accessed before the app is closed, so that I can restart from that location (+1) element.

Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56

1 Answers1

0

Store the last traversed index in shared preferences and next time initialize the iteration with value from shared pref.

Let's say index was 3, int lastTraversedIndex = 3;

prefs.editor.putInt("index", lastTraversedIndex).commit(); 

When you start app get value from pref

int lastTraversedIndex = prefs.getInt("index", 0); //0 for first run

Use this index

if(lastTraversedIndex < list.size()) {
 for(int i = lastTraversedIndex; i<list.size(); i++) {
  //
 }
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53