Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top