SharedPreferences Clear/Save

后端 未结 5 727
醉梦人生
醉梦人生 2020-12-09 02:57

Iam trying to make a checker and I want to save a value into SharedPreferences. But i\'am not sure if it works

This what I do to save the value is : *

相关标签:
5条回答
  • 2020-12-09 03:07

    // save string to SharedPreferences

    public static void saveStringToSharedPreferences(Context mContext, String key, String value) {
        if(mContext != null) {
            SharedPreferences mSharedPreferences = mContext.getSharedPreferences("SHARED_PREFERENCES", 0);
            if(mSharedPreferences != null)
                mSharedPreferences.edit().putString(key, value).commit();
        }
    }
    

    // read string from SharedPreferences

    public static String readStringFromSharedPreferences(Context mContext, String key) {
        if(mContext != null) {
            SharedPreferences mSharedPreferences = mContext.getSharedPreferences("SHARED_PREFERENCES", 0);
            if(mSharedPreferences != null)
                return mSharedPreferences.getString(key, null);
        }
        return null;
    }
    

    // remove from SharedPreferences

    public static void removeFromSharedPreferences(Context mContext, String key) {
        if (mContext != null) {
            SharedPreferences mSharedPreferences = mContext.getSharedPreferences(Constants.SHARED_PREFERENCES_NAME, 0);
            if (mSharedPreferences != null)
                mSharedPreferences.edit().remove(key).commit();
        }
    }
    
    0 讨论(0)
  • 2020-12-09 03:10

    To clear SharedPreferences use this

    SharedPreferences preferences = getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.clear(); 
    editor.commit();
    

    Hope this helped you.

    0 讨论(0)
  • 2020-12-09 03:11

    Simply you can:

    getSharedPreferences("PREFERENCE", 0).edit().clear().commit();
    
    0 讨论(0)
  • 2020-12-09 03:11

    For removing all preferences :

    SharedPreferences mPrefs_delete = context.getSharedPreferences(GeneralFunctions.SETTING_SINGLE_MASTER_CHILD, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor_delete = mPrefs_delete.edit();
    editor_delete.clear().commit();
    
    0 讨论(0)
  • 2020-12-09 03:12

    You are not using the same preferences. Take a while to read http://developer.android.com/reference/android/app/Activity.html

    In your first activity you are using:

    SharedPreferences prefs = getSharedPreferences("PREFERENCE", MODE_PRIVATE);
    

    And in the other activity clearing you are only using:

    SharedPreferences preferences = getPreferences(0);
    

    Reading the docs:

    Retrieve a SharedPreferences object for accessing preferences that are private to this activity. This simply calls the underlying getSharedPreferences(String, int) method by passing in this activity's class name as the preferences name.

    You need to use the same preference name in both activities. So in your second activity, where you do the clearing just use

    SharedPreferences preferences = getSharedPreferences("PREFERENCE", MODE_PRIVATE);
    
    0 讨论(0)
提交回复
热议问题