When user clicked on switch btn alertdialog will pop up asking if he wish to delete item or not. If he click yes switch btn will go to opposite side, and if he clicked no switch should remain same position. The problem is that if he click yes everything works fine bit if he clicked no, it seems like switch btn go on opposite side and after he clicked no it return on old position and again trigger code inside it and again alertdialog pop up and so it goes in a circle, and only way to exit that loop is to click on yes button. So my question is how to stop do this..i want that if he click on no my switch btn stay in the old position and not auto-triggering my code again, and again...I tried putting my code in onTouchListener, onCickeListener....
my code: `
aSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked){
AlertDialog.Builder dialogBuider = new AlertDialog.Builder(MainActivity.this);
dialogBuider.setMessage("Delete item?");
dialogBuider.setPositiveButton("Yes", (dialog, which) -> {
dialog.dismiss();
});
dialogBuider.setNegativeButton("No", (dialog, id) -> {
dialog.cancel();
});
dialogBuider.setOnCancelListener(dialog -> {
aSwitch.setChecked(false);
});
AlertDialog alertDialog = dialogBuider.create();
alertDialog.show();
}
else {
AlertDialog.Builder dialogBuider = new AlertDialog.Builder(MainActivity.this);
dialogBuider.setMessage("Delete Item?");
dialogBuider.setPositiveButton("Yes", (dialog, which) -> {
dialog.dismiss();
});
dialogBuider.setNegativeButton("No", (dialog, id) -> {
dialog.cancel();
});
dialogBuider.setOnCancelListener(dialog -> {
aSwitch.setChecked(true);
});
AlertDialog alertDialog = dialogBuider.create();
alertDialog.show();
}
});`
Well i found solution, if somebody run into the same problem ,just use
if (!buttonView.isPressed())
return;
inside setOnCheckedChangeListener()
so that you can check if switch btn is pressed if it's not then just retun