Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: prevent preference dialog for preference which must be loaded over the network

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?

like image 492
Noah Avatar asked Feb 02 '26 07:02

Noah


1 Answers

I was struggeling with the same problem but in a more simple environment. My PreferenceScreen is defined in an xml file. There is one Preference I want to handle myself. So I simply put a "Preference" object into the xml file instead of a "ListPreference" or "EditTextPreference"

    <Preference android:title="@string/preloadmaps1" android:summary="@string/preloadmaps2"
        android:key="preloadMaps" />

Now there is no more Editor connetcted with the Preference and I can savely handle the editing myself in "OnPreferenceClickListener"

like image 176
Gerhard Avatar answered Feb 04 '26 20:02

Gerhard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!