Reputation: 573
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 save 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?
Upvotes: 1
Views: 384
Reputation: 30168
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.
Upvotes: 0
Reputation: 74780
You can achieve this using next 2 steps:
Set preference to be not persistent via android:persistent="false"
in xml or via setPersistent(false)
in code. This way default SharedPreferences
will not be modified when user changes values in preference UI.
Handle value changes via OnPreferenceChangeListener
listener registered using setOnPreferenceClickListener
. This will allow you to intercept changes and handle them the way you need.
Upvotes: 3