views:

157

answers:

2

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
I'm already using it. However I just wondered how to set them up during installation.
OneWorld
@OneWorld: You cannot execute any code during installation.
CommonsWare
I'm sure about that. Thats why I'm asking if android provides so preferences setup in their installation.
OneWorld
+2  A: 

It is a good idea to check for the preference file existence anyway if you depend on certain critical values.

Octavian Damiean
Ok, thats right. However, somehow it must be possible to pack a predefined settings file in the apk-File
OneWorld
As far as I know you can only build an initialization method which basically creates the preferences file with your desired values.
Octavian Damiean
well, then I still have to put the preferences in program code ;(
OneWorld
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
I considered that too. But found that there must be a more convenient android native solution...
OneWorld