Accessing SharedPreferences through static methods

Cristian’s answer is good, but if you want to be able to access your shared preferences from everywhere the right way would be: Create a subclass of Application, e.g. public class MyApp extends Application {… Set the android:name attribute of your <application> tag in the AndroidManifest.xml to point to your new class, e.g. android:name=”MyApp” (so … 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

How secure are SQLite and SharedPreferences files on Android?

Is it possible for someone to grab them in any way? That depends on the someone. As Mr. Burov indicates, users of rooted phones can get at whatever they want. Ordinary users and other applications can’t, by default. It’s the not normally accessible part giving me additional grey hair 🙂 By default, files are secure. … Read more

Android SharedPreferences String Set – some items are removed after app restart

Based on your question, you should call commit only after 4 items have been added to the set. In your code, you are calling commit for each feedback which will overwrite the previous feedback. Update: http://developer.android.com/reference/android/content/SharedPreferences.html#getStringSet(java.lang.String, java.util.Set) Note that you must not modify the set instance returned by this call. The consistency of the stored … Read more

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