Search code examples
androidsharedpreferencesstoring-data

how to make my SharedPreferences object to save data after app closes?


I made an app that her main purpose is to save some sort of information(not important which information right now ), in my app I'm making a SharedPreferences object called sp and a SharedPreferences.Editor object called editor.

in my onCreate() method Im retrieving the stored HashSet like:

    Set<String> setFull = sp.getStringSet("SET_FULL", new HashSet<String>());

if it's null I get a new HashSet (on first use), and all of the information I need to store Im storing into it as different strings.

I need this Set to be acceptable from all my activities so I use:

   SharedPreferences.Editor editor = sp.edit();
   editor.putStringSet("SET_FULL", setFull);
   editor.apply();            //also tried .commit()

in each activity I need that set, so Im retrieving it as the above example.

on the main activity(the one I do all my manipulations on the HashSet), when the activity is open and alive, I can add to that HashSet and into my SharedPreferences items and edit it and everything, and when I travel between my activities I can see it with everything I added and everything is fine, but once I close my app(from my emulator / phone / android studio) when I reopen it, all the data is deleted and I need to add everything all over again.

I tried storing the set back into my editor in 4 different ways:

1: in my onCreate() method:

   editor.putStringSet("SET_FULL", setFull);
   editor.apply();            //also tried .commit()

2: overriding the method onDestroy() in my main activity:

@Override
protected void onDestroy() {
    super.onDestroy();
    editor.putStringSet("SET_FULL", setFull);
    editor.apply();
}

3: overriding the method onPause() in my main activity:

@Override
protected void onPause() {
    super.onPause();
    editor.putStringSet("SET_FULL", setFull);
    editor.apply();
}

4: overriding the method onStop() in my main activity:

@Override
protected void onStop() {
    super.onStop();
    editor.putStringSet("SET_FULL", setFull);
    editor.apply();
}

I also tried putting all 4 options( also tried only 2 or 3 and etc..) together one after the other in my main activity but that also didn't seems to work.

I also tried swapping between .commit() to .apply() (on my SharedPreferences.Editor object when I use the .putStringSet("", set) on it) but both gave same problem, no more no less.

I didn't got any errors and no warnings or problems( no crashes no nothing ) , it just seems to delete everything once the app closes no matter which way. I've been looking for solutions for this for the past week, also asked friends( who suggested the same as the internet which was the overriding methods ) but none seems to work, yet everywhere I Read or I ask everyone tells me that a SharedPreferences object should keep the data I Store into it even after my app closes.


Solution

  • I solved my problem thanks to @Henry answer and based on some extra research I've done, I will write the 2 things that solved my problem. the two solutions are: the first is based on @Henry answer: instead of manipulating on the HashSet I get from the SharedPreferences object, copying it(the object itself and not a reference) like that:

    Set<String> newSet = new HashSet(setFull);
    

    the second one is before every-time I put something again inside the SharedPreferences object adding this command:

     editor.clear();
    

    and that solves my entire problem, it saves the data in the SharedPreferences after the app closes and everything.

    thanks to everyone that helped!