문제

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

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top