3

I've been trying to build a task list in Android, and I want it to remember what is in the list even if the application is closed. I've been trying to do this using onSaveInstanceState as follows:

public class Main extends Activity implements OnClickListener, OnKeyListener {
    ...
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ...
        if (savedInstanceState != null) {
            ToDo = savedInstanceState.getStringArrayList("MyArrayList");
            AA.notifyDataSetChanged();
        } else {
            ToDo = new ArrayList<String>(); // Initiate ToDo
        }
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putStringArrayList("MyArrayList", ToDo);
    }
    // ...To-Do implementation follows
}

But this doesn't work, so I started reading about onSaveInstanceState and if I understand it right, then you actually cannot save data between sessions with it. So how can you do it then?

Andrey Ermakov
  • 3,298
  • 1
  • 25
  • 46
Zero
  • 1,864
  • 3
  • 21
  • 39

3 Answers3

7

Check out the Android documentation about Data Storage for your options for persisting data. You can use Shared Preferences, a SQLite database, files in a private internal directory, or files on the SD card (if available).

Basically, onSaveInstanceState is used for the scenario where Android kills off your activity to reclaim memory. In that scenario, the OS will keep a record of your activity's presence, in case the user returns to it, and it will then pass the Bundle from onSaveInstanceState to your onCreate method. It's not meant to be used to general purpose storage.

wsanville
  • 37,158
  • 8
  • 76
  • 101
0

I would put my todos in a DB, and save data in the DB in 'onPause()'. You might want to read up on the Activity lifecycle for a description when an activity loose it's data etc.

Also this thread could be of interest in regards to onSaveInstanceState.

Community
  • 1
  • 1
Qben
  • 2,617
  • 2
  • 24
  • 36
0

onSaveInstanceState() is called every time the activity is destroyed and recreated, for example during an orientation change or when the device is running on low memory. Use it for saving state variables and taking appropriate action in case activity is killed and recreated.

What you need is SharedPreferences, to persist data even when application is closed and reopened. Take a look at this :

    //Initialize the list
    Set<String> valueSet = new HashSet<>();
    valueSet.add("A");
    valueSet.add("B");

    //Saving data in sharedPreferences
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor =  sharedPreferences.edit();
    editor.putStringSet("key_name", valueSet);
    editor.commit();

    //Retrieving saved data from sharedPreferences (even in later sessions)
    valueSet = sharedPreferences.getStringSet("key_name", null);