My app has a ListPreference, whose entries come from a network API. In my PreferenceActivity's onCreate(), I spawn a background thread which makes the API call and then populates the entries of the ListPreference after one or two seconds.
If the user clicks the ListPreference button on the preference screen before the options have been downloaded, I want to prevent the preference dialog from showing and instead notify the user that the list of options is still being loaded.
I suspect that correct approach is to override the OnPreferenceClickListener, like this:
ListPreference dpref = (ListPreference) findPreference("debug");
String[] s = {"one", "two", "three"};
dpref.setEntries(s);
dpref.setEntryValues(s);
dpref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Toast.makeText(this, "hi there", Toast.LENGTH_SHORT).show();
return true;
}
});
The toast gets shown, but the ListPreference chooser dialog is shown as well. The OnPreferenceClickListener documentation says that onPreferenceClick should return true
if the click was handled, but returning false
has the same result.
How do I prevent the preference dialog from showing?
Is there a better way to handle preferences whose options must be downloaded before viewing?
I'm not sure why your way is not working, but here is a quick workaround solution:
Add stub Preference for each preference that should be downloaded. You can customize you action on click in any way you want. No dialogs will be shown.
When your preference options are ready, remove old Preference (by its name) and create new ListPreference (with the same name as just removed one) with your options.
This will give you a flexibility to add any kind of custom preferences on any event you need. Though as you can see, some extra coding is required.