How to delete shared preferences data from App in Android

前端 未结 24 2172
日久生厌
日久生厌 2020-11-22 14:37

How do I delete SharedPreferences data for my application?

I\'m creating an application that uses a lot of web services to sync data. For testing purposes, I need to

相关标签:
24条回答
  • 2020-11-22 15:25

    You can always do it programmatically as suggested by the other answers over here. But for development purpose, I find this Plugin very helpful as it speeds up my development significantly.

    PLUGIN: ADB Idea

    It provides you with features to Clear App Data and Revoke Permission from your Android Studio itself, just with click of a button.

    enter image description here

    0 讨论(0)
  • 2020-11-22 15:26

    To clear all SharedPreferences centrally from any class:

    public static SharedPreferences.Editor getEditor(Context context) {
        return getPreferences(context).edit();
    }
    

    And then from any class: (commit returns a Boolean where you can check whether your Preferences cleared or not)

    Navigation.getEditor(this).clear().commit();
    

    Or you can use apply; it returns void

    Navigation.getEditor(this).clear().apply();
    
    0 讨论(0)
  • 2020-11-22 15:26

    You can use preferences.edit().remove("key").commit() to delete saved values from shared preferences.

    0 讨论(0)
  • 2020-11-22 15:27

    In the class definitions:

    private static final String PREFERENCES = "shared_prefs";
    
    private static final SharedPreferences sharedPreferences  = getApplicationContext().getSharedPreferences(PREFERENCES, MODE_PRIVATE);
    

    Inside the class:

    public static void deleteAllSharedPrefs(){
        sharedPreferences.edit().clear().commit();
    }
    
    0 讨论(0)
  • 2020-11-22 15:27

    Just did this this morning. From a command prompt:

    adb shell
    cd /data/data/YOUR_PACKAGE_NAME/shared_prefs
    rm * // to remove all shared preference files
    rm YOUR_PREFS_NAME.xml // to remove a specific shared preference file
    

    NOTE: This requires a rooted device such as the stock Android virtual devices, a Genymotion device, or an actual rooted handset/tablet, etc.

    0 讨论(0)
  • 2020-11-22 15:30
    • To remove a particular value,

    SharedPreferences.Editor remove(String key) followed by a commit() or a apply()

    • To remove all the values,

      SharedPreferences.Editor clear() followed by a commit() or a apply()

    0 讨论(0)
提交回复
热议问题