views:

34

answers:

1

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 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?

+1  A: 

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.
Pentium10
This looks right but it reads like it creates a file after you edit and commit a change and possibly keeps changing this file as you make more changes. What I want is the file to save an instant snapshot of the preferences so when I change prefs it leaves that file alone and then I can read it back and all prefs are instantly back when I made the initial call. Can I make the call getSharedPreferences('pref1',0), then commit 1 pref, and then disassociate the file from changes until I need to call it later?
Tim Wayne