Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MultiSelectListPreference uncheck does not fire SharedPreferenceChanged

I'm using a MultiSelectListPreference with the following properties..

<MultiSelectListPreference        
 android:key="shop_list"               
 android:entries="@array/shop_titles"
 android:entryValues="@array/shop_values"
     android:title="@string/pref_title_shops"   
     android:dialogTitle="@string/pref_title_shops" 
 android:defaultValue="@array/shop_values"
 android:summary="Choose where you hunt"
 />   

The preferences are inside a static PreferenceFragment, inside a common Activity. I m registering and unregistering the listener for the SharedPreferenceChanged event on the onPause and onResume of the PreferenceFragment.

code excerpt:

public class AgentSettingsActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
    }

    public static class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener
    {
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            Utils.Log("onCreate");

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.pref_general);

        }

        @Override
        public void onResume()
        {
            Utils.Log("onResume");
                    super.onResume();
            try
            {
                Utils.Log("Add pref listeners and try set summary");
               getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);

            }
            catch (Exception e)
            {
                Utils.Log("Pref not found");
            }

        }

        @Override
        public void onPause()
        {
            super.onPause();
            Utils.Log("onPause");

            // Unregister the listener whenever a key changes
            getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);

        }

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
        {
            Utils.Log("onPreferenceChange, key : " + key);    
        }
    }

}

When i check an option, the event fires everytime until I start unchecking them.. Then the listener stops getting called. Any ideas what I m missing here? Does this have smth to the with the way i use the Fragment / Activity ?

NOTE: I should also add that I have a ListPreference which continues to work even after the MultiSelectListPref stops responding properly.

like image 250
Orkun Ozen Avatar asked Oct 21 '22 16:10

Orkun Ozen


1 Answers

I am dealing with the same issue. From SDK 18 all thing seems to work fine but in rest, I have same issue like you.

I found a solution that seems to work for me:

    MultiSelectListPreference multiSelectListPref = (MultiSelectListPreference) findPreference("pref_name");
    if (multiSelectListPref != null) {
        multiSelectListPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                MultiSelectListPreference mpreference = (MultiSelectListPreference) preference;
                mpreference.setSummary(newValue.toString());
                mpreference.setValues((Set<String>) newValue);
                return true;
            }
        });
    }

maybe it is useful for your cases too

like image 136
Calin Martinconi Avatar answered Oct 29 '22 18:10

Calin Martinconi