14mcei February 2016

Android Shared Preference Still visible after Deleting File

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 ?

Answers


Rahul February 2016

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();
    }


Amit Vaghela February 2016

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.

Post Status

Asked in February 2016
Viewed 2,094 times
Voted 10
Answered 2 times

Search




Leave an answer