2

Class file

import android.support.v14.preference.SwitchPreference;
    //...

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    addPreferencesFromResource(R.xml.pref_blank);
    this.preferenceScreen = getPreferenceScreen();
}

   public void resetDevices(ArrayList<DeviceAdapterItem> items) {
        preferenceScreen.removeAll();
        preferenceScreen.setPersistent(false);

        for(DeviceAdapterItem i : items) {

            ExpandablePreferenceCategory category = buildCategory(i);
            preferenceScreen.addPreference(category);

            Preference deviceTypePreference = buildDeviceTypePreference(i);
            deviceTypePreference.setOnPreferenceChangeListener(this);
            category.addPreference(deviceTypePreference);

            Preference monitoring = buildMonitoringPreference(i);
            monitoring.setOnPreferenceChangeListener(this);
            category.addPreference(monitoring);

            Preference wifiOnly = buildWifiOnlyPreference(i);
            wifiOnly.setOnPreferenceChangeListener(this);
            category.addPreference(wifiOnly);

            Preference delete = buildDeletePreference(i);
            delete.setOnPreferenceChangeListener(this);
            category.addPreference(delete);

            category.handleCollapseExpand();
        }
    }

    private Preference buildMonitoringPreference(DeviceAdapterItem i) {
        SwitchPreference monitoring = new SwitchPreference(context.getSupportActionBar().getThemedContext());
        monitoring.setPersistent(false);
        monitoring.setTitle(getString(R.string.monitoring));
        monitoring.setKey("some bogus value" + i.getDeviceId());

        monitoring.setChecked(i.isTakeSnapshots());
        monitoring.setSummary(getString(i.isTakeSnapshots() ? R.string.enabled : R.string.disabled ));
        return monitoring;
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        Log.d("pchange", preference.getTitle() + " = " + newValue.toString());
        if(preference instanceof SwitchPreference) {
            Log.d("pchange", "its a switch!");
            ((SwitchPreference) preference).setChecked((boolean)newValue);
        }
        return true;
    }

pref_blank.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:persistent="false"
    >

</PreferenceScreen>

The switch appears turned off (when it should be turned on). When I click on the switch in the UI, onPreferenceChange gets triggered and the newValue does toggle. The UI never changes though, it's always stuck in the off state?

CamHart
  • 3,825
  • 7
  • 33
  • 69

2 Answers2

1

If you use

implementation 'com.android.support:preference-v7:27.1.1'

You should use SwitchPreferenceCompat like this

<SwitchPreferenceCompat
            android:key="taskSendNotification"
            android:title="@string/notificationTask"
            android:defaultValue="true"></SwitchPreferenceCompat>

android.support.v14.preference.SwitchPreference will not work with 'com.android.support:preference-v7:27.1.1'

Ucdemir
  • 2,852
  • 2
  • 26
  • 44
0

It is because you have android:persistent set to false. Set it to true and the UI should change accordingly.

iTurki
  • 16,292
  • 20
  • 87
  • 132
  • 1
    Setpersistence should only impact the value saving to a local file. I set the values myself, loading them from a backend. Or is there a dependency in v14 for some strange reason? – CamHart Sep 05 '17 at 15:45
  • @CamHart My understanding is that the UI only updates its preferences if the preference's `android:persistent` is set to `true`. – iTurki Sep 05 '17 at 18:57