Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

java - My settings activity (using shared preferences) is not saving data, it is showing default values only

I am currently working on an android app, for which I created a settings activity. I used shared preferences, but it isn't working as it is supposed to. I open the settings page, toggle the switches, remove the app from memory, but when I open it again, the default settings are shown. I have no idea why. Please help. Also, I am a beginner, since I have started coding quite recently, so forgive me if I made a silly mistake.

public class SettingsActivity extends AppCompatActivity {

    public static final String SETTINGS_PREFERENCES = "com.example.taskmasterv3.SettingsPreferences";
    Switch switchReminder, switchNotifications;
    boolean reminders;
    boolean notifications;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        switchReminder = findViewById(R.id.switchReminder);
        switchNotifications = findViewById(R.id.switchNotifications);

        SharedPreferences prefs = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE);
         boolean reminder = prefs.getBoolean("reminders",true );
         boolean notification = prefs.getBoolean("notifications", false);

        switchReminder.setChecked(reminder);
        switchNotifications.setChecked(notification);





        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();

        }






    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.7k users

...