Storing data in SharedPreferences accessible to all activities

android, sharedpreferences

Solution

Yes. SharePreferences do exactly this. In every activity you can this:

SharedPreferences prefs = getSharedPreferences(ApplicationConstants.PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(name, value);
editor.commit();

And then retrieve values in other activty doing this:

mPrefs.getString(name, "");

This is the documentation: http://developer.android.com/reference/android/content/SharedPreferences.html

And this is a good example to start with:

http://myandroidsolutions.blogspot.it/2012/03/android-preferenceactivity.html

Problem

I want to store and retrieve data that is accessible to all activities in my app using SharedPreferences. Is that possible? Up until now I have been doing it such that the data is stored for a particular activity.

Original source

Related problems