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

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

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

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

How to save List to SharedPreferences?

It only possible to use primitive types because preference keep in memory. But what you can use is serialize your types with Gson into json and put string into preferences: private static SharedPreferences sharedPreferences = context.getSharedPreferences(STORE_FILE_NAME, Context.MODE_PRIVATE); private static SharedPreferences.Editor editor = sharedPreferences.edit(); public <T> void setList(String key, List<T> list) { Gson gson = new … Read more