2

I am trying an application where I am using Shared Preference. When I delete the preference file from data/data/com.your.package.name/shared_prefs/mySharedPref.xml manually using Android monitor, still the app is able to read the preference values.

I am assuming that some how the value is retained in main memory of the phone. Am I correct & what is the viable solution to clear shared preferences totally leaving no traces. But one thing I want to clear preference only if the file is wiped. For this I need to check presence of file, Any other approach rather than checking with File class ?

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
14mcei
  • 43
  • 5

2 Answers2

1

I think this code must work

 public static void clearAllPreference(Context context){
        SharedPreferences prefs = context.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.clear();
        editor.commit();
    }
Rahul
  • 367
  • 2
  • 6
  • 27
1

To remove specific values: SharedPreferences.Editor.remove() followed by a commit()

To remove them all SharedPreferences.Editor.clear() followed by a commit()

You use remove() to remove specific preferences, you use clear() to remove them all.

Checkout official documentation on SharedPreferences.Editor.

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142