1// load file
2SharedPreferences sharedPref = getSharedPreferences(PREFERENCES_FILENAME, MODE_PRIVATE);
3
4// read value
5String value = sharedPref.getString(PREFERENCE_KEY, null);
6
7// write value
8sharedPref.edit()
9 .putString(PREFERENCE_KEY, value)
10 .apply();
11
1SharedPreferences sharedPref = getSharedPreferences("name", Context.MODE_PRIVATE);
2SharedPreferences.Editor editor = sharedPref.edit();
3editor.putString("key", "Value");
4editor.commit();
1boolean mBoolean = PreferenceManager.getDefaultSharedPreferences(yourContext).getBoolean(key, defaultValue); //getBoolean will return defaultValue is key isn't found
2//you can also use getInt, getFloat, getLong, getString, getStringSet and change the variable type, of course
3
1// MY_PREFS_NAME - a static String variable like:
2//public static final String MY_PREFS_NAME = "MyPrefsFile";
3SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
4 editor.putString("name", "Elena");
5 editor.putInt("idName", 12);
6 editor.apply();
7
1boolean committed = PreferenceManager.getDefaultSharedPreferences(yourContext).edit().putBoolean(key, mValue).commit();
2//you can also use putInt, putFloat, putLong, putString, putStringSet and change the value of mValue
3//committed is true if everything went alright, false if there was an error