Difference between getDefaultSharedPreferences and getSharedPreferences

getDefaultSharedPreferences will use a default name like “com.example.something_preferences”, but getSharedPreferences will require a name. getDefaultSharedPreferences in fact uses Context.getSharedPreferences (below is directly from the Android source): public static SharedPreferences getDefaultSharedPreferences(Context context) { return context.getSharedPreferences(getDefaultSharedPreferencesName(context), getDefaultSharedPreferencesMode()); } private static String getDefaultSharedPreferencesName(Context context) { return context.getPackageName() + “_preferences”; } private static int getDefaultSharedPreferencesMode() { return Context.MODE_PRIVATE; }

How do you save/store objects in SharedPreferences on Android?

You can use gson.jar to store class objects into SharedPreferences. You can download this jar from google-gson Or add the GSON dependency in your Gradle file: implementation ‘com.google.code.gson:gson:2.8.8’ you can find latest version here Creating a shared preference: SharedPreferences mPrefs = getPreferences(MODE_PRIVATE); To save: MyObject myObject = new MyObject; //set variables of ‘myObject’, etc. Editor … Read more

Android Shared preferences for creating one time activity (example) [closed]

Setting values in Preference: // MY_PREFS_NAME – a static String variable like: //public static final String MY_PREFS_NAME = “MyPrefsFile”; SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); editor.putString(“name”, “Elena”); editor.putInt(“idName”, 12); editor.apply(); Retrieve data from preference: SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); String name = prefs.getString(“name”, “No name defined”);//”No name defined” is the default value. int idName = prefs.getInt(“idName”, … Read more

Save ArrayList to SharedPreferences

After API 11 the SharedPreferences Editor accepts Sets. You could convert your List into a HashSet or something similar and store it like that. When you read it back, convert it into an ArrayList, sort it if needed and you’re good to go. //Retrieve the values Set<String> set = myScores.getStringSet(“key”, null); //Set the values Set<String> … Read more

How do I get the SharedPreferences from a PreferenceActivity in Android?

import android.preference.PreferenceManager; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); // then you use prefs.getBoolean(“keystring”, true); Update According to Shared Preferences | Android Developer Tutorial (Part 13) by Sai Geetha M N, Many applications may provide a way to capture user preferences on the settings of a specific application or an activity. For supporting this, Android provides a simple … Read more

What’s the difference between commit() and apply() in SharedPreferences

apply() was added in 2.3, it commits without returning a boolean indicating success or failure. commit() returns true if the save works, false otherwise. apply() was added as the Android dev team noticed that almost no one took notice of the return value, so apply is faster as it is asynchronous. http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply()

How to use SharedPreferences in Android to store, fetch and edit values [closed]

To obtain shared preferences, use the following method In your activity: SharedPreferences prefs = this.getSharedPreferences( “com.example.app”, Context.MODE_PRIVATE); To read preferences: String dateTimeKey = “com.example.app.datetime”; // use a default value using new Date() long l = prefs.getLong(dateTimeKey, new Date().getTime()); To edit and save preferences Date dt = getSomeDate(); prefs.edit().putLong(dateTimeKey, dt.getTime()).apply(); The android sdk’s sample directory contains … Read more