开发者

Android: Set Shared Preferences during Installation?

Does Android provide smth. like that OR开发者_如何学C 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?"


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜