Reputation: 1936
I have a preference screen where I can change some settings. In the code I can edit them via the shared preferences without a problem.
I have 2 questions: - do these settings stay saved somewhere when the phone restarts? - can I insert other settings into the sharedprefs. I mean settings that are not declared in the preference screen.
Upvotes: 0
Views: 583
Reputation: 1532
As stated before, SharedPreferences are persisted (written into an xml file) so they will always be available even if you restart (so long as you call commit() on the Editor belonging to the SharedPreference).
As far as inserting other settings into the sharedprefs,
You can actually have multiple SharedPreferences (in different files) if you want to be more organized, but you can definitely save anything you want in them.
To get the default shared preference for the activities context:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
To get one of many SharedPreference that you created, use this:
SharedPreferences prefs = context.getSharedPreferences(String name, int mode)
name being the name of the file mode being MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, MODE_MULTI_PROCESS depending on how you want other applications to be able to access your apps prefs which can be useful
Upvotes: 0
Reputation: 1079
do these settings stay saved somewhere when the phone restarts?
Yes they are persisted in an xml file on the device.
can I insert other settings into the sharedprefs?
Yes you can have lots of settings in sharedprefs, the only ones that are displayed or modified through the PreferenceActivity are those you set in your layout file and their keys will be the keys specified in the layout file.
Upvotes: 1