Android GetSharedPreferences Code Example


Example 1: android save int

public void saveInt(int val, String key){     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mainActivity);     SharedPreferences.Editor editor = prefs.edit();     editor.putInt(key, val);     editor.apply(); }  public int getInt(String key){     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mainActivity);     int val = prefs.getInt(key, 0);     return val; }

Example 2: android sharedpreferences

SharedPreferences sharedPref = getSharedPreferences("name", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putString("key", "Value"); editor.commit();

Example 3: set preference value android

boolean committed = PreferenceManager.getDefaultSharedPreferences(yourContext).edit().putBoolean(key, mValue).commit(); //you can also use putInt, putFloat, putLong, putString, putStringSet and change the value of mValue //committed is true if everything went alright, false if there was an error

Comments

Popular posts from this blog

Chemistry - Bond Angles In NH3 And NCl3

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Change The Font Size Of Visual Studio Solution Explorer