0

SharedPreferences in the below code does not work as required. Where am I going wrong? The SharedPreferences is saved on click, and the same is values are being retrieved OnCreate and OnResume in my Android activity.

Code:

mySwitch = (SwitchCompat) findViewById(R.id.atten);
strttime = (TextView) findViewById(R.id.textView5);
endtime = (TextView) findViewById(R.id.textView6);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
silent = settings.getBoolean("switchkey", false);
mySwitch.setChecked(silent);

mySwitch.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        final SwitchCompat btn = (SwitchCompat) v;
        final boolean switchChecked = silent;

        String message = "Are you sure you want to Logout?";
        if (!btn.isChecked()) {
            message = "Are you sure you want to Login?";
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.MyAlertDialogStyle); // Change "this" to `getActivity()` if you're using this on a fragment
        builder.setMessage(message).setPositiveButton("Ok", new DialogInterface.OnClickListener() {@Override
            public void onClick(DialogInterface dialog, int i) {
                // "Yes" button was clicked
                if (switchChecked) {

                    Log.d("You are :", "Checked");

                    SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putBoolean("switchkey", true);
                    editor.apply();
                } else {
                    SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putBoolean("switchkey", false);
                    editor.apply();
                }
            }
        }).setNegativeButton("Cancel", null).show();
    }
});
Zoe
  • 27,060
  • 21
  • 118
  • 148
Mahesh CS
  • 9
  • 6

2 Answers2

0

Why do you seemingly have two different SwitchCompat Views, called mySwitch and btn? It's hard to tell what you're trying to accomplish.

Anyways, try this instead for grabbing the state of the SwitchCompat:

builder.setMessage(message)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putBoolean("switchkey", mySwitch.isChecked());
                        //or maybe you want btn.isChecked()? I don't know
                        editor.apply();
                    }
Gavin Wright
  • 3,124
  • 3
  • 14
  • 35
0

This is happening because of final boolean switchChecked = silent; inside your setOnClickListener

you passing silent in switchChecked and silent is always false due to this silent = settings.getBoolean("switchkey", false); (if no value is available)

so all you have to do is pass correct value in switchChecked like this

final boolean switchChecked = btn.isChecked();

because you need to check a user pressed your switch or not

now switchChecked will have right value according user-input

so your code will be

  mySwitch = (SwitchCompat) findViewById(R.id.atten);
    strttime = (TextView) findViewById(R.id.textView5);
    endtime = (TextView) findViewById(R.id.textView6);
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    silent = settings.getBoolean("switchkey", false);
    mySwitch.setChecked(silent);

    mySwitch.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final SwitchCompat btn = (SwitchCompat) v;
            final boolean switchChecked = btn.isChecked();

            String message = "Are you sure you want to Logout?";
            if (!btn.isChecked()) {
                message = "Are you sure you want to Login?";
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setMessage(message)
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int i) {
                            // "Yes" button was clicked
                            if (switchChecked) {

                                Log.d("You are :", "Checked");

                                SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
                                SharedPreferences.Editor editor = settings.edit();
                                editor.putBoolean("switchkey", true);
                                editor.apply();
                            } else {
                                SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
                                SharedPreferences.Editor editor = settings.edit();
                                editor.putBoolean("switchkey", false);
                                editor.apply();
                            }

                        }
                    })
                    .setNegativeButton("Cancel", null)
                    .show();

        }

    });
Ashwini Saini
  • 1,324
  • 11
  • 20