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?"
3 Answers
It is a good idea to check for the preference file existence anyway if you depend on certain critical values.

- 39,405
- 19
- 98
- 102
-
Ok, thats right. However, somehow it must be possible to pack a predefined settings file in the apk-File – OneWorld Sep 06 '10 at 12:12
-
As far as I know you can only build an initialization method which basically creates the preferences file with your desired values. – Octavian Helm Sep 06 '10 at 12:25
-
well, then I still have to put the preferences in program code ;( – OneWorld Sep 06 '10 at 12:39
-
1Well 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 Helm Sep 06 '10 at 13:03
-
I considered that too. But found that there must be a more convenient android native solution... – OneWorld Sep 06 '10 at 14:25
-
It really is a good idea to check for the preferences on every start on Android. Because, the user can erease "User data" all times when the app is installed. So this answer gets marked. – OneWorld Dec 28 '10 at 19:59
If you worry that user can clear data of your app, consider using this flag inside tag <application>
of your AndroidManifest.xml:
android:manageSpaceActivity="path.to.your.activity"
Then, instead of "Clear data" button, it will be "Manage space". And your activity will be called when the user click that button.
Generally, you should do as Select0r mentioned. All other "default xml/ ..." are not worth for the time you maintain your project.
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

- 12,234
- 11
- 45
- 68
-
I'm already using it. However I just wondered how to set them up during installation. – OneWorld Sep 06 '10 at 11:59
-
I'm sure about that. Thats why I'm asking if android provides so preferences setup in their installation. – OneWorld Sep 06 '10 at 12:40