Storing Array List Object in SharedPreferences

Convert your array or object to Json with Gson library and store your data as String in json format. Save; SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); Editor editor = sharedPrefs.edit(); Gson gson = new Gson(); String json = gson.toJson(arrayList); editor.putString(TAG, json); editor.commit(); Read; SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); Gson gson = new Gson(); String json = sharedPrefs.getString(TAG, “”); … Read more

Android – How Do I Set A Preference In Code

I assume by preferences you are referring to your application’s preferences and not Android phone settings. To store preferences between runs of you application you need to do the following Create a SharedPreferences object SharedPreferences settings = getSharedPreferences(String n, MODE_PRIVATE); String n identifies your preferences and the second argument is the mode they’ll be accessed … Read more

Store a List or Set in SharedPreferences

I found the easiest solution to store and retrieve a list of items from SharedPreferences is to simply serialize / deserilaize the array into / from JSON and store it into a string setting. Gson comes really handy doing it. READ: SharedPreferences prefs = context.getSharedPreferences(“settings”, Context.MODE_PRIVATE); String value = prefs.getString(“list”, null); GsonBuilder gsonb = new … Read more

Android SharedPreferences , how to save a simple int variable [duplicate]

SharedPreferences sp = getSharedPreferences(“your_prefs”, Activity.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putInt(“your_int_key”, yourIntValue); editor.commit(); the you can get it as: SharedPreferences sp = getSharedPreferences(“your_prefs”, Activity.MODE_PRIVATE); int myIntValue = sp.getInt(“your_int_key”, -1); The SharedPreference interface gives you access to the an xml file, and a easy way to modify it through its editor. The file is stored in /data/data/com.your.package/shared_prefs/ … 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

Shared Preferences reset data when app is force closed or device is restarted

I have a login screen and wanted the app to appear as if it’s remained “logged in” at the internal screen after the app is closed/destroyed/phone call/etc. I have a Preferences Object to save values following Login or Register. I read preference values in all the key screen onResume() methods. After login (for example): SharedPreferences … Read more