Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RingtonePreference not firing OnPreferenceChangeListener

Every other preference I have fires its OnPreferenceChangeListener. However, my RingtonePreference it doesn't:

p = getPreferenceScreen().findPreference("pref_tone");
String rname = preferences.getString("pref_tone",Settings.System.DEFAULT_RINGTONE_URI.toString());
String name = ringtoneToName(rname);
p.setSummary(name);
p.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
    // Never hits here!
    String v = (String) newValue;
    preference.setSummary(ringtoneToName(v));           
    return true;
}
});
like image 332
LiteWait Avatar asked Apr 04 '12 13:04

LiteWait


2 Answers

onActivityResult had to call super.onActivityResult is the fix

like image 89
LiteWait Avatar answered Nov 10 '22 22:11

LiteWait


Note that a RingtonePreference uses an Activity for the ringtone picker.

If you are using a RingtonePreference in a support PreferenceFragment (android.support.v4.preference.PreferenceFragment) then the RingtonePreference ends up erroneously using the parent Activity instead of the PreferenceFragment when calling startActivityForResult. This means that the onActivityResult callback gets called on the parent Activity as well. The easiest workaround to fix this problem is to override onActivityResult in the parent Activity and make sure it forwards the callback to the PreferenceFragment. For example like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Fragment f = getSupportFragmentManager().findFragmentByTag(PREFERENCE_FRAGMENT_TAG);
    if (f != null) {
        f.onActivityResult(requestCode, resultCode, data);
    }
}
like image 2
Fabian Frank Avatar answered Nov 11 '22 00:11

Fabian Frank