androidandroid-togglebuttonswitchcompat

Restore state of toggle button on canceling dialog


I have a toggle button in my activity. When switch is On, it shows enable dialog and switch is Off, it shows disable dialog.

((SwitchCompat) findViewById(R.id.toggleButton)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                ((StyleableTextView) findViewById(R.id.lbl_toggle_status)).setText("on");
                showEnableSharingDialog();
            } else {
                ((StyleableTextView) findViewById(R.id.lbl_toggle_status)).setText("off");
                showDiasableShareingDialog();
            }
        }
    });

This is enable dialog:

private void showEnableShareingDialog() {
    final CharSequence options[]= getResources().getStringArray(R.array.sharing_options);
    AlertDialog.Builder builder = new AlertDialog.Builder(SettingsActivity.this);

     builder.setTitle(getString(R.string.you_want_to_enable_prev_sigtings));

    builder.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if(which==0){
                mShareSigtings=true;
                mEnableShareing=true;
                updateSettings();
            } else if(which==1){
                mShareSigtings=true;
                mEnableShareing=false;
                updateSettings();
            } else {
                /*---todo---*/
                dialog.cancel();
            }
        }
    });
    builder.show();
}

This is disable dialog:

private void showDiasableShareingDialog() {
    final CharSequence options[]= getResources().getStringArray(R.array.remove_sharing_options);
    AlertDialog.Builder builder = new AlertDialog.Builder(SettingsActivity.this);

    builder.setTitle(getString(R.string.you_want_to_remove_prev_sigtings));

    builder.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which == 0) {
                mShareSigtings = false;
                mRemovePrevSigtings = true;
                updateSettings();
            } else if (which == 1) {
                mShareSigtings = false;
                mRemovePrevSigtings = false;
                updateSettings();
            } else {
                /*---todo---*/
                dialog.cancel();
            }
        }
    });
    builder.show();
}

In both dialogs, toggle button switches it state i.e, say toggle was in ON state then I press the button, dialog pops up, when i press CANCEL it goes from ON to OFF state. Expected result was when CANCEL is pressed, toggle button should remain on its original position i.e, if toggle is ON then on pressing CANCEL it should remain ON. How am I going to achieve this kind of operation?

Also, if i do something like below both the dialogs continuously pops up on pressing CANCEL.

if(((SwitchCompat)findViewById(R.id.toggle3)).isChecked()){
                    ((SwitchCompat)findViewById(R.id.toggle3)).setChecked(false);
}else {
                    ((SwitchCompat)findViewById(R.id.toggle3)).setChecked(true);
}

Solution

  • You have to know that:

    So, a fast fix for this, before you change the check status of the swich set null as onCheckedChanged then put the listener again. Here is an example:

     SwitchCompat mySwich = ((SwitchCompat) findViewById(R.id.toggleButton))   
         OnCheckedChangeListener listener = new OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked){
                            ((StyleableTextView) findViewById(R.id.lbl_toggle_status)).setText("on");
                            showEnableSharingDialog();
                        } else {
                            ((StyleableTextView) findViewById(R.id.lbl_toggle_status)).setText("off");
                            showDiasableShareingDialog();
                        }
                    }
                };
    mySwitch.setOnCheckedChangeListener(listener);
    

    And on your CANCEL callback, you can do something like that:

    mySwitch.setOnCheckedChangeListener(null);
    if(((SwitchCompat)findViewById(R.id.toggle3)).isChecked()){
                        ((SwitchCompat)findViewById(R.id.toggle3)).setChecked(false);
    }else {
                        ((SwitchCompat)findViewById(R.id.toggle3)).setChecked(true);
    }
    mySwitch.setOnCheckedChangeListener(listener);