When I click on an unchecked checkbox, it gets checked and a dialog appears with Save and Cancel buttons (and other objects). When the user clicks Save, data is saved, checkbox stays checked, everyone is happy. When the user clicks Cancel, the checkbox gets unchecked, the dialog is dismissed but the else condition becomes active (opening the dialog in the else part) creating an infite loop where I click Cancel in the diagram, it is dismissed, but then the first diagram is shown and so on. How to break this loop after I click on the Cancel button that makes the checkbox unchecked/checked?
chPassword.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isChecked()) {
Log.i("cb status", "true");
dialog_newpass = new Dialog(Settings.this);
dialog_newpass.setContentView(R.layout.dialog_password_seton);
dialog_newpass.setTitle(" Set new password ");
dialog_newpass.setCancelable(true);
et_newpass = (EditText) dialog_newpass.findViewById(R.id.EditText_Password);
et_confirmpass = (EditText) dialog_newpass.findViewById(R.id.EditText_Password2);
btn_SavePass = (Button) dialog_newpass.findViewById(R.id.btn_savePass);
btn_SavePass.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
SavePassword("showpassword", "true");
dialog_newpass.dismiss();
}
}
});
btn_CancelPass = (Button) dialog_newpass.findViewById(R.id.btn_cancelPass);
btn_CancelPass.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
dialog_newpass.dismiss();
chPassword.setChecked(false); //due to this, the else condition becomes active
}
});
dialog_newpass.show();
}
else
{
Log.i("cb status", "false");
dialog_confirmpass = new Dialog(Settings.this);
dialog_confirmpass.setContentView(R.layout.dialog_password_setoff);
dialog_confirmpass.setTitle(" Enter password ");
dialog_confirmpass.setCancelable(true);
et_pass = (EditText) dialog_confirmpass.findViewById(R.id.EditText_Password);
btn_SavePass = (Button) dialog_confirmpass.findViewById(R.id.btn_savePass);
btn_SavePass.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
SavePassword("showpassword", "false");
dialog_confirmpass.dismiss();
}
}
});
btn_CancelPass = (Button) dialog_confirmpass.findViewById(R.id.btn_cancelPass);
btn_CancelPass.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
dialog_confirmpass.dismiss();
chPassword.setChecked(true); //due to this, the if condition becomes active
}
});
dialog_confirmpass.show();
}
}
});
You could add a boolean to your activity class:
Then set it to true in your cancel handler:
And then check for it inside your else block: