24.04.2011 19:35, Jake Colman пишет:
Kostya,
I like your second suggestion.
So in my Preference Activity's onCreate() I should get every preference by
original key name and then reset the key name to include the widget.
Basically, yes.
That means I'll have a full set of non-unique preference keys without
any data but that should not really be an issue.
Not necessarily - if you tweak all the keys inside onCreate, before any
preference values are saved, I don't think you will have any orphaned value.
According to the docs it does not look like there is a method to iterate
through "all preferences" so, instead, I'll have to get every preference
by specific key name and manually change each one. Have I missed a more
direct way of changing all the keys?
PreferenceGroup (which PreferenceScreen is a subclass of) has "int
getPreferenceCount()" / "getPreference(int index)".
If I use an onClickListener() and on SharedPreferenceChangeListener() to
detect specific preference clicks or changes to a preference, I can
parse out the widget ID from the key so that I know which specific
widget has been effected. I can then make sure that I update just that
specific widget accordingly. Can you think of a better way to do that?
Umm... Sounds like you also want to present options for multiple widgets
at the same time within a single preference activity?
If so, I would not use the activity as the event listener.
I'd create a class that encapsulates the complete set of preferences for
one widget instance, serves as the event listener for the involved
Preference objects, and knows its associated widget Id value. The
activity would instantiate these objects as needed, one for each widget Id.
I'd also use onPreferenceChange rather that onSharedPreferenceChanged,
because the former uses a Preference reference you can switch on,
without parsing preference keys.
-- Kostya
To facilitate parsing out the widget id, I have already externalized all
my key names as string resources so that I can edit the string to
include a ":". This way I can parse out the text that follows the ":"
and get the widget id. Did you do something similar or did you not have
the need to know which widgetid has had its preference changed?
My widgets are so simple, I don't use PreferenceActivity to configure
them :)
-- Kostya
...Jake
"KV" == Kostya Vasilyev<[email protected]> writes:
KV> Jake,
KV> It's possible to make your subclass of PreferenceActivity use a
KV> specific preference file by putting something like this inside
KV> its onCreate:
KV> PreferenceManager prefManager = getPreferenceManager();
KV> prefManager.setSharedPreferencesName("widget_pref_" +
KV> String.valueOf(widgetId));
KV> This seems somewhat inefficient though, because each widget will
KV> get its own preference storage file.
KV> To use a single preference file where each widget gets its own
KV> set of keys (dependent on a particular widget id), you could try
KV> using Preference.setKey:
KV> for preference : all Preferences in the screen {
KV> String keyOriginal = preference.getKey();
KV> String keyWithWidgetId = keyOriginal + String.valueOf(widgetId);
KV> preference.setKey(keyWithWidgetId);
KV> }
KV> Hope this helps.
KV> -- Kostya
KV> 24.04.2011 17:28, Jake Colman пишет:
>> I currently use a Preference Activity and a preference XML file to
>> manage my preferences. Since a Preference Activity handles edits
>> on its own, is there still a way to use it if I need to change the
>> key name at runtime to have a widget id? I am pretty sure I can
>> get the widget from the intent since it is launched as a
>> configuration activity but can I tap in and use it?
KV> --
KV> Kostya Vasilyev -- http://kmansoft.wordpress.com
KV> --
KV> You received this message because you are subscribed to the Google
KV> Groups "Android Developers" group.
KV> To post to this group, send email to
[email protected]
KV> To unsubscribe from this group, send email to
KV> [email protected]
KV> For more options, visit this group at
KV> http://groups.google.com/group/android-developers?hl=en
--
Kostya Vasilyev -- http://kmansoft.wordpress.com
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en