I want to ask the user to confirm the change of a CheckBoxPreference in the settings of my application. I tried to put an AlertDialog in both OnPreferenceClickListener and OnSharedPreferenceChangeListener but I did not succeed in CANCELLING the CheckBoxPreference change once the preference is clicked. Thanks for your help.
Nathan, I tried the following 2 ideas :
1) Implement OnPreferenceClickListener in PreferenceActivity like this :
@Override
public boolean onPreferenceClick(Preference preference) {
new AlertDialog.Builder(this)
.setTitle("Move Data to SD card")
.setMessage("This will ...... Are you sure?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
CheckBoxPreference checkBoxPref = (CheckBoxPreference)findPreference(getString(R.string.prefSDEpisodes_key));
checkBoxPref.setChecked(!checkBoxPref.isChecked() );
}
})
.show();
return false;
}
Actually it works but I am not satisfied with this solution because the user sees the state of the CheckBoxPrefence changing in the background behind the Dialog Box. This is just a workaround that consists in swapping back the box. This is not clean at all. I need to get the control before the change.
2) Implement OnSharedPreferenceChangeListener in PreferenceActivity with similar code in onSharedPreferenceChanged() but in this case the swapping back of the box value in setNegativeButton() programmatically triggers a preference change event again and again. The only way to get rid of the confirmation dialog box is to choose YES.
If there is no simple solution I am ready to extend CheckBoxPrefence but how? Which method should I override?