简体   繁体   中英

Cancel switch on AlertDialog dismiss

So basically when user click on switch button an AlertDialog will pop up asking if he is sure to do that..if he said yes switch button will...well switch to opposite side as it suppose to do, but if he said no..my switch button should remain position. The problem is that when i click switch button AlertDialog pop up and if i click yes it works fine... but when i click no...my switch button however change position. my code:

    if (binding.optionsSwitch.isChecked()) {
                SharedPreferences.Editor editor = context.getSharedPreferences("save", Context.MODE_PRIVATE).edit();
                editor.putBoolean("value", true);
                editor.apply();
                binding.optionsSwitch.setChecked(true);
         
                AlertDialog.Builder dialogBuider = new AlertDialog.Builder(context);
                dialogBuider.setMessage("Are you sure you want to delete the selected item?");
                dialogBuider.setPositiveButton("Delete item", (dialog, which) -> {
                    viewModel.mtDeleteCumulativeResponse().observe(getViewLifecycleOwner(), objectObserver);
                    viewModel.deleteCumulative();
                    viewModel.getAssets();
                    dialog.dismiss();
                });

               dialogBuider.setNegativeButton("Quit", (dialog, id) -> {
                    dialog.cancel();
               // binding.optionsSwitch.setChecked(true);
            });
            dialogBuider.setOnCancelListener(dialog -> {

                binding.optionsSwitch.setChecked(false);
            });
                // Create and show the AlertDialog
                AlertDialog alertDialog = dialogBuider.create();
                alertDialog.show();
                

            } else {

                SharedPreferences.Editor editor = context.getSharedPreferences("save", Context.MODE_PRIVATE).edit();
                editor.putBoolean("value", false);
                editor.apply();
                binding.optionsSwitch.setChecked(false);
                AlertDialog.Builder dialogBuider = new AlertDialog.Builder(context);
                dialogBuider.setMessage("Are you sure you want to delete the selected item?");
                dialogBuider.setPositiveButton("Delete item", (dialog, which) -> {
                    dialog.dismiss();
 
});
                dialogBuider.setNegativeButton("Quit", (dialog, id) -> {
                dialog.cancel();
            });

            dialogBuider.setOnCancelListener(dialog -> {
            binding.optionsSwitch.setChecked(true); });
                // Create and show the AlertDialog
                AlertDialog alertDialog = dialogBuider.create();
                alertDialog.show();

and my xml: `

<Switch
                android:id="@+id/options_switch"
                android:layout_width="0dp"
                android:layout_height="19dp"
                android:layout_marginStart="8dp"
                android:background="@color/bela"
                android:padding="6dp"
                android:paddingStart="26dp"
                android:text="Detailed Item List"
                android:textColor="#8fa2b0"
                android:textSize="9sp"
                android:checked="true"
                app:layout_constraintBottom_toTopOf="@+id/rv_cash_acc_list"
                app:layout_constraintEnd_toStartOf="@+id/list_details_swtch"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/rv_cash_acc_list"
                app:layout_constraintWidth_percent="0.55"
                tools:ignore="UseSwitchCompatOrMaterialXml" />`

The best way to achieve this is to add an OnDismissListener or an OnCancelListener. something like alertDialog.setOnCancelListener(dialog -> { binding.optionsSwitch.setChecked(false); });

if (binding.optionsSwitch.isChecked()) {
    /*...*/
    dialogBuider.setNegativeButton("Quit", (dialog, id) -> {
        if (dialog != null) {
            dialog.dismiss();
            binding.optionsSwitch.setChecked(false);
            //False here, you want to undo the check action in your switch.
        }
    });
    /*...*/
}
else {
    /*...*/
    dialogBuider.setNegativeButton("Quit", (dialog, id) -> {
        if (dialog != null) {
            dialog.dismiss();
            binding.optionsSwitch.setChecked(true);
            //Here should be true, now is not checked but you want to undo it and check it.
         }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM