SharedPreferences versus private file
In my application, I am using a structure PreferenceActivity
to store persistent data. I intend to create multiple save files, all of which can be accessed Preference
s, but only one at a time.
When is it better to use a private file generated Context.openFileOutput()
and when is it better to use it SharedPreferences
?
EDIT
My data exists exclusively in primitives.
source to share
Typically, developers use a preference file that is common to the entire application using getDefaultSharedPreferences
.
However, Android has a method getSharedPreferences(String name, int mode)
on Context . You can use this to have multiple preference files, in your case, save the files using unique names passed to the parameter name
.
As far as volatility is concerned, you can force preferences to persist by getting Editor
through edit()
and then calling commit()
.
Make sure SharedPreferences will indeed be shared based on name
:
Get and save the contents of the preferences file "name", returning SharedPreferences through which you can get and change your values. Only one instance of the SharedPreferences object is returned to all callers with the same name, meaning they will see each other's changes as soon as they are done.
source to share