Search code examples
savesharedpreferencessetvalue

Reset value from sharedPreferences


My app has a lot of classes and i save the score into sharePref individually.Every class has its own sharePref.In the end i made a class that i sum all these sharePref into an integer named totalscore ,something like this https://gyazo.com/3f0f9da2c64e587dd6244cf3933f957c .Eventually it works.

I want to clear the values of sharePreferences when i press the restart button because it keeps the high score.

Thank you for your time and sorry for my Engish.


Solution

  • The way usually I manage sharedPreferences in my projects is that, I create a class named AppPreferences and then create getters and setters methods in it for reading and writing some preferences. This helps me manage all the preferences also all user preferences are at one place.

    import android.content.Context;
    import android.content.SharedPreferences;
    
    public class AppPreferences {
     private static AppPreferences mAppPreferences;
     protected Context mContext;
     private SharedPreferences mSharedPreferences;
     private SharedPreferences.Editor mSharedPreferencesEditor;
    
     private AppPreferences(Context context) {
      mContext = context;
      mSharedPreferences = context.getSharedPreferences("AppPreferences", Context.MODE_PRIVATE);
      mSharedPreferencesEditor = mSharedPreferences.edit();
     }
    
     /**
      * Creates single instance of AppPreferences
      *
      * @param context - context of Activity or Service
      * @return Returns instance of Class AppPreferences
      */
     public static synchronized AppPreferences getInstance(Context context) {
    
      if (mAppPreferences == null) {
       mAppPreferences = new AppPreferences(context.getApplicationContext());
      }
      return mAppPreferences;
     }
    
     /**
      * Stores String value in preference
      *
      * @param key - key of preference
      * @param value - value for that key
      */
     public void setValue(String key, String value) {
      mSharedPreferencesEditor.putString(key, value);
      mSharedPreferencesEditor.commit();
     }
    
     /**
      * Stores int value in preference
      *
      * @param key - key of preference
      * @param value - value for that key
      */
     public void setValue(String key, int value) {
      mSharedPreferencesEditor.putInt(key, value);
      mSharedPreferencesEditor.commit();
     }
    
     /**
      * Stores Double value in String format in preference
      *
      * @param key - key of preference
      * @param value - value for that key
      */
     public void setValue(String key, double value) {
      setValue(key, Double.toString(value));
     }
    
     /**
      * Stores long value in preference
      *
      * @param key - key of preference
      * @param value - value for that key
      */
     public void setValue(String key, long value) {
      mSharedPreferencesEditor.putLong(key, value);
      mSharedPreferencesEditor.commit();
     }
    
     /**
      * Stores boolean value in preference
      *
      * @param key - key of preference
      * @param value - value for that key
      */
     public void setValue(String key, boolean value) {
      mSharedPreferencesEditor.putBoolean(key, value);
      mSharedPreferencesEditor.commit();
     }
    
     /**
      * Retrieves String value from preference
      *
      * @param key - key of preference
      * @param defaultValue - default value if no key found
      */
     public String getStringValue(String key, String defaultValue) {
      return mSharedPreferences.getString(key, defaultValue);
     }
    
     /**
      * Retrieves int value from preference
      *
      * @param key - key of preference
      * @param defaultValue - default value if no key found
      */
     public int getIntValue(String key, int defaultValue) {
      return mSharedPreferences.getInt(key, defaultValue);
     }
    
     /**
      * Retrieves long value from preference
      *
      * @param key - key of preference
      * @param defaultValue - default value if no key found
      */
     public long getLongValue(String key, long defaultValue) {
      return mSharedPreferences.getLong(key, defaultValue);
     }
    
     /**
      * Retrieves boolean value from preference
      *
      * @param keyFlag - key of preference
      * @param defaultValue default value if no key found
      */
     public boolean getBoolanValue(String keyFlag, boolean defaultValue) {
      return mSharedPreferences.getBoolean(keyFlag, defaultValue);
     }
    
     /**
      * Removes key from preference
      *
      * @param key - key of preference that is to be deleted
      */
     public void removeKey(String key) {
      if (mSharedPreferencesEditor != null) {
       mSharedPreferencesEditor.remove(key);
       mSharedPreferencesEditor.commit();
      }
     }
    
    
     /**
      * Clears all the preferences stored
      */
     public void clear() {
      mSharedPreferencesEditor.clear().commit();
     }
    }
    

    Now you can use the above example anywhere in your project and when you want to remove something just call removeKey function and pass the key.

    SIDE NOTE: Create a Constants.java file in your project and add all the keys and static variables in it, that way you don't have to worry about keys spellings and all.