K
K
Krasavchik262020-05-07 10:44:05
Java
Krasavchik26, 2020-05-07 10:44:05

What is the correct way to use SharedPreferences for Switch?

It is necessary that when you press the button every time you press it, the value changes, and when you restart the application, this value remains.
Change of value works now. But it is not saved.
I added SharedPreferences, but I know it's not correct, I took it from saving in the settings for strings. But how to do for switch?

def_pref = PreferenceManager.getDefaultSharedPreferences(this);
        String text_shar = def_pref.getString("key", "Шар");
        if (text_shar != null) {
                status_pub++;
                switch (status_pub) {
                    case 0:
                        button.setText("");
                        status_pub = 0;
                        break;
                    case 1:
                        button.setText("AAA");
                        status_pub = 1;
                        break;
                    case 2:
                        button.setText("BBB");
                        status_pub = 2;
                        break;
                    case 3:
                        button.setText("CCC");
                        status_pub = 0;
                        break;
                }
            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2020-05-07
@Krasavchik26

SharedPreferences#getBoolean

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question