0

I am implementing code whereby the user can uncheck an option in the Preferences.

If that happens, I launch a dialog with a message to the user. If the user clicks on Confirm the preference would remain with that changed value and the App would close. That works fine.

But if the user clicks Cancel, I revert the value of the preference back to true programmatically, but the change is not reflected on the screen (the option appears as disabled).

My question is: is there a way to visibly update that item, so that it fires when the user clicks Cancel (since the value of the item will return to true in that case)?

Or, is there a way to undo the last change to the SharedPreferences and have this reflected immediately in the GUI?

This is my code:

Fragment

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.pref_general, rootKey);
        Preference acceptTerms = findPreference(PREF_ACCEPT);

        acceptTerms.setOnPreferenceClickListener(
                arg0 -> {
                    showConfirm();
                    return true;
                });
    }

    private void showConfirm() {
        MaterialAlertDialogBuilder materialAlertDialogBuilder = new MaterialAlertDialogBuilder(getActivity());
        materialAlertDialogBuilder.setTitle(DIALOG_LEGAL_TITLE);
        materialAlertDialogBuilder.setMessage(DIALOG_LEGAL_BODY);
        materialAlertDialogBuilder.setPositiveButton(DIALOG_LEGAL_OK,
                (dialogInterface, i) -> closeApp());
        materialAlertDialogBuilder.setNegativeButton(DIALOG_LEGAL_CANCEL,
                (dialogInterface, i) -> updatePreference());
        materialAlertDialogBuilder.show();
    }

    private void closeApp() {
        if (Build.VERSION.SDK_INT < 21) {
            Objects.requireNonNull(getActivity()).finishAffinity();
        } else {
            Objects.requireNonNull(getActivity()).finishAndRemoveTask();
        }
    }

    /*
       Here the preference is set to true internally 
       but not changed in the GUI. 
       My idea is to find a way to make it activate in the GUI
    */
    private void updatePreference(){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(PREF_ACCEPT, true);
        editor.apply();
    }
}

XML

    <SwitchPreference
        android:key="accept_terms"
        android:title="@string/pref_accept_terms"
        android:summary="@string/pref_description_accept_terms"
        android:defaultValue="false"
        />
A. Cedano
  • 557
  • 7
  • 39
  • Still in Java? Please, subscribe to SharedPreferences, see https://stackoverflow.com/questions/2542938/sharedpreferences-onsharedpreferencechangelistener-not-being-called-consistently, https://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener. Probably you don't need SP to reflect changes. – CoolMind Jan 29 '22 at 12:56
  • Add logic when calling the updatePreference method that changes the UI accordingly to what you want. Since this is a small snippet of your application and I don't know where exactly is the place that you display the change, you can use [LiveData](https://developer.android.com/topic/libraries/architecture/livedata). – tomerpacific Jan 29 '22 at 12:57

0 Answers0