store and retrieve a class object in shared preference

Yes we can do this using Gson Download Working code from GitHub SharedPreferences mPrefs = getPreferences(MODE_PRIVATE); For save Editor prefsEditor = mPrefs.edit(); Gson gson = new Gson(); String json = gson.toJson(myObject); // myObject – instance of MyObject prefsEditor.putString(“MyObject”, json); prefsEditor.commit(); For get Gson gson = new Gson(); String json = mPrefs.getString(“MyObject”, “”); MyObject obj = … Read more

Should accessing SharedPreferences be done off the UI Thread?

I’m glad you’re already playing with it! Some things to note: (in lazy bullet form) if this is the worst of your problems, your app’s probably in a good spot. 🙂 Writes are generally slower than reads, though, so be sure you’re using SharedPreferenced$Editor.apply() instead of commit(). apply() is new in GB and async (but … Read more

Shared preferences for creating one time activity

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

How can I view the shared preferences file using Android Studio?

The Device File Explorer that is part of Android Studio 3.x is really good for exploring your preference file(s), cache items or database. Shared Preferences /data/data//shared_prefs directory It looks something like this To open The Device File Explorer: Click View > Tool Windows > Device File Explorer or click the Device File Explorer button in … Read more

Where are shared preferences stored?

SharedPreferences are stored in an xml file in the app data folder, i.e. /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml or the default preferences at: /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml SharedPreferences added during runtime are not stored in the Eclipse project. Note: Accessing /data/data/<package_name> requires superuser privileges