Is it okay to use manual callback methods to initialize the default values?

I ran into Android customization activity (to support devices below 3.0, not the new preference snippet)

In the code example below, to display the currently selected value in the summary for the first time, they make a call onPreferenceChange(..)

, which in this case is a manual callback method.

Although I figured out the whole process, it took me a long time to figure out what was going on because I am new to android and I have never used to call listeners like actionPerformed(...)

in a class ActionListener

, etc. / p>

Is this bad practice? , any good alternatives to this?

SettingsActivity.class

public class SettingsActivity extends PreferenceActivity
        implements Preference.OnPreferenceChangeListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
// Add 'general' preferences, defined in the XML file
    addPreferencesFromResource(R.xml.pref);

// For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
// updated when the preference changes.
        bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));//used here similar to a view id
        //getString(R.String...) extracts the string value from strings.xml
    }
    /**
     * Attaches a listener so the summary is always updated with the preference value.
     * Also fires the listener once, to initialize the summary (so it shows up before the value
     * is changed.)
     */
    private void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
        preference.setOnPreferenceChangeListener(this);
// Trigger the listener immediately with the preference's
// current value.
        onPreferenceChange(preference,
                PreferenceManager
                        .getDefaultSharedPreferences(preference.getContext())
                        .getString(preference.getKey(), ""));
    }
    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();
        if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference 'entries' list (since they have separate labels/values).
            ListPreference listPreference = (ListPreference) preference;
            int prefIndex = listPreference.findIndexOfValue(stringValue);
            if (prefIndex >= 0) {
                preference.setSummary(listPreference.getEntries()[prefIndex]);
            }
        } else {
// For other preferences, set the summary to the value simple string representation.
            preference.setSummary(stringValue);
        }
        return true;
    }
}

      

+3
java android design listener android-preferences


source to share


No one has answered this question yet

Check out similar questions:

673
How to call a method after a delay in Android
13
Try using Window.FEATURE_CUSTOM_TITLE but got an exception: you cannot combine custom titles with other title functions.
7
Adding Firebase Social Media Sharing Logic to Android
4
getFragmentManager returns null pointer exception
2
setText on a button from another kind of android activity
2
get selected preference value in android
1
It is bad practice to call the listener method manually
0
java.lang.NullPointerException for static field in android library project
0
cannot bind OnPreferenceChangeListener to my preference
0
How do I create a PreferenceScreen in a PreferenceActivity?



All Articles
Loading...
X
Show
Funny
Dev
Pics