Does Android provide smth. like that OR do I have to check every time during start of my app "Oh, am I freshly installed? Do I have to initialize the Shared Preferences now?"
A:
You can just get any key/value-pair from the preferences and provide a default-value in the method-call, like this: prefValue = prefs.getString("prefName", "defaultValue");
, there's no need to initialize.
If the key ("prefName" in this case) doesn't exist in the preferences, the default-value will be used. Once you let the user change the prefs, the PreferencesActivity will take care of writing the changed values back to the prefs.
Here's a good tutorial on Android Preferences:
http://www.kaloer.com/android-preferences
Select0r
2010-09-06 11:54:19
I'm already using it. However I just wondered how to set them up during installation.
OneWorld
2010-09-06 11:59:18
@OneWorld: You cannot execute any code during installation.
CommonsWare
2010-09-06 12:11:55
I'm sure about that. Thats why I'm asking if android provides so preferences setup in their installation.
OneWorld
2010-09-06 12:40:39
+2
A:
It is a good idea to check for the preference file existence anyway if you depend on certain critical values.
Octavian Damiean
2010-09-06 11:56:49
Ok, thats right. However, somehow it must be possible to pack a predefined settings file in the apk-File
OneWorld
2010-09-06 12:12:15
As far as I know you can only build an initialization method which basically creates the preferences file with your desired values.
Octavian Damiean
2010-09-06 12:25:00
Well one more thing I have thought of is that you can include an XML file with your preset values and then parse them on initialization. This however implies building a parser. The question is, is it worth the effort?
Octavian Damiean
2010-09-06 13:03:09
I considered that too. But found that there must be a more convenient android native solution...
OneWorld
2010-09-06 14:25:37