Is it ok to save a JSON array in SharedPreferences?

The JSON object in Java does not implement serialaizable out of the box. I have seen others extend the class to allow that but for your situation I would simply recommend storing the JSON object as a string and using its toString() function. I have had success with this. editor.putString(“jsondata”, jobj.toString()); And to get it … Read more

How to use Shared Preferences in MVP without Dagger and not causing Presenter to be Context dependent?

Your Presenter should not be Context dependent in the first place. If your presenter needs SharedPreferences you should pass them in the constructor. If your presenter needs a Repository, again, put that in the constructor. I highly suggest watching Google clean code talks since they do a really good job explaining why you should use … Read more

How to mock a SharedPreferences using Mockito

So, because SharedPreferences comes from your context, it’s easy: final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class); final Context context = Mockito.mock(Context.class); Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs); // no use context for example, for getValidToken(Context context), the test could be: @Before public void before() throws Exception { this.sharedPrefs = Mockito.mock(SharedPreferences.class); this.context = Mockito.mock(Context.class); Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs); } @Test public void testGetValidToken() throws … Read more

Android – How do I get sharedpreferences from another activity?

Use the following functions to add shared preferences and to fetch the saved values from all activities. public static void setDefaults(String key, String value, Context context) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = preferences.edit(); editor.putString(key, value); editor.apply(); // or editor.commit() in case you want to write data instantly } public static String getDefaults(String key, … Read more

How to Iterate through all Bundle objects

Could you save everything as String using the toString() method? Don’t know if primitive types are mapped to their Object equivalents (e.g. int to class Integer), but if they are, then you might be able to do something like this, instead of laboriously checking each possible class. for (String key : bundle.keySet()) { saveKeyValueInPrefs(key, bundle.get(key).toString()); … Read more

Get Android shared preferences value in activity/normal class

If you have a SharedPreferenceActivity by which you have saved your values SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String imgSett = prefs.getString(keyChannel, “”); if the value is saved in a SharedPreference in an Activity then this is the correct way to saving it. SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE); SharedPreferences.Editor editor = shared.edit(); editor.putString(keyChannel, email); editor.commit();// commit is … Read more

Android – Storing/retrieving strings with shared preferences

To save to preferences: PreferenceManager.getDefaultSharedPreferences(context).edit().putString(“MYLABEL”, “myStringToSave”).apply(); To get a stored preference: PreferenceManager.getDefaultSharedPreferences(context).getString(“MYLABEL”, “defaultStringIfNothingFound”); Where context is your Context. If you are getting multiple values, it may be more efficient to reuse the same instance. SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String myStrValue = prefs.getString(“MYSTRLABEL”, “defaultStringIfNothingFound”); Boolean myBoolValue = prefs.getBoolean(“MYBOOLLABEL”, false); int myIntValue = prefs.getInt(“MYINTLABEL”, 1); And if … 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