Preferences layout for non preferneces data
I have an activity which is used for editing some object. Currently its layout as a simple form but I'd like it to be more similar to preferences activity. I'd like to use the same layouts which are available there (e.g. CheckBoxPreference) but s开发者_运维百科ave changes to my object rather than to preferences. Is there an easy way to have an activity which looks like preferences but doesn't use preferences at the back back-end?
You can achieve this using next 2 steps:
Set preference to be not persistent via
android:persistent="false"
in xml or viasetPersistent(false)
in code. This way defaultSharedPreferences
will not be modified when user changes values in preference UI.Handle value changes via
OnPreferenceChangeListener
listener registered usingsetOnPreferenceClickListener
. This will allow you to intercept changes and handle them the way you need.
Yes, you can provide your own implementation of SharedPreferences
and save your data to somewhere that is not preferences, while still using the same UI and XML definition. See this question, and this link. The sample link is pretty complex since it is writing data back to the database directly, but it's very easy to replace with a plain Map
and then retrieve the data from the Map into your object. Or, if you want the data saved immediately, just write it straight into your object.
精彩评论