Android ArrayList of custom objects – Save to SharedPreferences – Serializable?

Yes, you can save your composite object in shared preferences. Let’s say.. Student mStudentObject = new Student(); SharedPreferences appSharedPrefs = PreferenceManager .getDefaultSharedPreferences(this.getApplicationContext()); Editor prefsEditor = appSharedPrefs.edit(); Gson gson = new Gson(); String json = gson.toJson(mStudentObject); prefsEditor.putString(“MyObject”, json); prefsEditor.commit(); ..and now you can retrieve your object as: SharedPreferences appSharedPrefs = PreferenceManager .getDefaultSharedPreferences(this.getApplicationContext()); Gson gson = new … Read more

How to iterate through all keys of shared preferences?

What you can do is use getAll() method of SharedPreferences and get all the values in Map<String,?> and then you can easily iterate through. Map<String,?> keys = prefs.getAll(); for(Map.Entry<String,?> entry : keys.entrySet()){ Log.d(“map values”,entry.getKey() + “: ” + entry.getValue().toString()); } For more you can check PrefUtil.java’s dump() implementation.

how to use getSharedPreferences in android

First get the instance of SharedPreferences using SharedPreferences userDetails = context.getSharedPreferences(“userdetails”, MODE_PRIVATE); Now to save the values in the SharedPreferences Editor edit = userDetails.edit(); edit.putString(“username”, username.getText().toString().trim()); edit.putString(“password”, password.getText().toString().trim()); edit.apply(); Above lines will write username and password to preference Now to to retrieve saved values from preference, you can follow below lines of code String userName … Read more

Is it possible to add an array or object to SharedPreferences on Android

Regardless of the API level, Check String arrays and Object arrays in SharedPreferences SAVE ARRAY public boolean saveArray(String[] array, String arrayName, Context mContext) { SharedPreferences prefs = mContext.getSharedPreferences(“preferencename”, 0); SharedPreferences.Editor editor = prefs.edit(); editor.putInt(arrayName +”_size”, array.length); for(int i=0;i<array.length;i++) editor.putString(arrayName + “_” + i, array[i]); return editor.commit(); } LOAD ARRAY public String[] loadArray(String arrayName, Context mContext) … Read more

How to listen for preference changes within a PreferenceFragment?

I believe you just need to register/unregister the Listener in your PreferenceFragment and it will work. @Override public void onResume() { super.onResume(); getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); } @Override public void onPause() { getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); super.onPause(); } Depending on what you want to do you may not need to use a listener. Changes to the preferences are committed to SharedPreferences … Read more

Android SharedPreferences in Fragment

The method getSharedPreferences is a method of the Context object, so just calling getSharedPreferences from a Fragment will not work…because it is not a Context! (Activity is an extension of Context, so we can call getSharedPreferences from it). So you have to get your applications Context by // this = your fragment SharedPreferences preferences = … Read more

How to save HashMap to Shared Preferences?

I use Gson to convert HashMap to String and then save it to SharedPrefs private void hashmaptest() { //create test hashmap HashMap<String, String> testHashMap = new HashMap<String, String>(); testHashMap.put(“key1”, “value1”); testHashMap.put(“key2”, “value2”); //convert to string using gson Gson gson = new Gson(); String hashMapString = gson.toJson(testHashMap); //save in shared prefs SharedPreferences prefs = getSharedPreferences(“test”, MODE_PRIVATE); … Read more