开发者

Can I save SharedPreferences to file so I can have multiple instances of the prefs?

I love how SharedPreferences work in android and I would like to know if there is an easy way to save them to another file so I could load a previous instance of the prefs and vice versa. Basically when you load this file, all the preferences would change to how you had it before at 开发者_JAVA技巧once. I want to be able to swap 3 or 4 different versions of the same pref keys in this way. Is there an easy way to do this?


Yes it's possible. Context has a method called getSharedPreferences (String name, int mode)

Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.

Parameters
name    Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()).
mode    Operating mode. Use 0 or MODE_PRIVATE for the default operation, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions.

Returns
Returns the single SharedPreferences instance that can be used to retrieve and modify the preference values.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜