3

I have a method as follows:

public static void addHighligtedDate(String date){
        prefs = context.getSharedPreferences(Fields.SHARED_PREFS_FILE, 0);
        Set<String> highlightedDates = prefs.getStringSet(Fields.HIGHLIGHTED_DATES, new HashSet<String>());
        highlightedDates.add(date);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putStringSet(Fields.HIGHLIGHTED_DATES, highlightedDates);
        editor.commit();
    }  

Now the scenario is this:
When I open the app add the dates to be highlighted, they are highlighted because SharedPreferences contains the values. When I press the home button to exit the app and come back, the values are still there.

However, when the app is removed from recents, the values go away. Is this normal behavior or am I doing something wrong?

Going over the docs:

This data will persist across user sessions (even if your application is killed).

An SO User
  • 24,612
  • 35
  • 133
  • 221

1 Answers1

3

SharedPreferences is always deleted along with the app uninstall.

When you uninstall any application all the changes the application have made in your internal memory are revoked, that means your SharedPreference files, Other data files, Database file, Application gets removed automatically by the Android OS.

Check - how-to-remove-shared-preference-while-application-uninstall-in-android.

UPDATE:

However, when the application is killed or closed, the values from SharedPreferences persists. There is some problem in your code.

Change the method to -

public static void addHighligtedDate(String date){
        prefs = context.getSharedPreferences(Fields.SHARED_PREFS_FILE, 0);
        Set<String> highlightedDates = prefs.
        getStringSet(Fields.HIGHLIGHTED_DATES, new HashSet<String>());
        highlightedDates.add(date);
        SharedPreferences.Editor editor = prefs.edit();
        editor.clear();
        editor.putStringSet(Fields.HIGHLIGHTED_DATES, highlightedDates);
        editor.commit();
    }  

UPDATE:

public abstract Set getStringSet (String key, Set defValues)

Retrieve a set of String values from the preferences.

Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.

Parameters

key The name of the preference to retrieve.

defValues Values to return if this preference does not exist.

Also look for reference - sharedpreferences-does-not-save-on-force-close.

Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
  • 1
    This is not an app uninstall. This is remove from recents. Long press the HOME button and swipe the app off from the list. :) – An SO User Jul 09 '14 at 10:16
  • That will remove ALL other values from the preferences – An SO User Jul 09 '14 at 11:19
  • 1
    That is so sad ! What other options do I have to store key-value pairs easily? – An SO User Jul 09 '14 at 11:44
  • You should create common methods to `get preferences`, to `save preferences` and to `remove preferences` in some util class. Like that you just need to invoke them rather using again and again in different classes. For get and set, the parameters will be key/value and for remove the parameter will only be key. – sjain Jul 09 '14 at 11:56