lundi 22 juillet 2019

Checkbox state always stay checked

If I open the activity, the checkbox always stay checked and even if I unchecked it and leave the activity or closed the app it'll stay checked again after restarting the activity.

I've tried saving the state of the activity using the below code snippet.

checkBox1.setChecked(getSharedPreferences("NSUK", Context.MODE_PRIVATE).getBoolean("checkBox1", true));

        checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                getSharedPreferences("NSUK", Context.MODE_PRIVATE).edit().putBoolean("checkBox", isChecked).apply();

            }
        });

The checkbox is expected to always be in the state that the user leave it (checked or unchecked).




Aucun commentaire:

Enregistrer un commentaire