0

I'm new in android,I want to know is possible to store boolean and integer values in same shared preference.

1 Answers1

0
/**
     * Save value to shared preference.
     *
     * @param key     On which key you want to save the value.
     * @param value   The value which needs to be saved.
     * @param context the context
     * @description To save the value to a preference file on the specified key.
     */
    public synchronized void saveValue(String key, long value, Context context) {

        SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor saveValue = prefs.edit();
        saveValue.putLong(key, value);
        saveValue.apply();
    }

    /**
     * Gets value from shared preference.
     *
     * @param key          The key from you want to get the value.
     * @param defaultValue Default value, if nothing is found on that key.
     * @param context      the context
     * @return the value from shared preference
     * @description To get the value from a preference file on the specified
     * key.
     */
    public synchronized String getValue(String key, String defaultValue, Context context) {

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getString(key, defaultValue);
    }

Although if you want to save POJO's in shared preferences go for GSON

Lakhwinder Singh
  • 6,799
  • 4
  • 25
  • 42