I have get where you are getting error. You are actually unable to set that data.
if (switchReminder.isChecked())
{
reminders = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("reminders", reminders);
editor.apply();
}
if (switchNotifications.isChecked())
{
notifications = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("notifications", notifications);
editor.apply();
}
It directly called when you start the activity. But, this function doesn't called on changing state of switch.
So, you have to add a listener like following source code.
switchReminder.setOnClickListener(new View.OnClickListener() {
//enter your code here
if (switchReminder.isChecked())
{
reminders = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("reminders", reminders);
editor.apply();
}
})
Add setOnClickListener()
on switchNotifications
also. Than, I hope it will work.
edited :
//setOnClickListener
switchReminder.setOnClickListener(new View.OnClickListener() {
if (switchReminder.isChecked())
{
reminders = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("reminders", reminders);
editor.apply();
}
})
That was for only switchRemider
. and, following source code is for switchNotification
.
//setOnClickListener
switchNotifications.setOnClickListener(new View.OnClickListener() {
if (switchNotifications.isChecked())
{
notifications = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("notifications", notifications);
editor.apply();
}
})
You have add both code instead of
if (switchReminder.isChecked())
{
reminders = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("reminders", reminders);
editor.apply();
}
if (switchNotifications.isChecked())
{
notifications = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("notifications", notifications);
editor.apply();
}