Android switch state change

I am currently developing an application in which there is a menu, and one of the parameters in the menu is “Settings”, where the user can, in principle, decide to turn off sounds and other similar things. I currently have two switches in the settings. Here is the Java code for the settings activity:

import android.support.v7.app.ActionBarActivity; public class Options extends ActionBarActivity { private Switch ding; private Switch countdown; public boolean isDingChecked; public boolean isCountdownChecked; public static final String PREFS = "examplePrefs"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_options); ding = (Switch) findViewById(R.id.switch1); ding.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { SharedPreferences examplePrefs = getSharedPreferences(PREFS,0); Editor editor = examplePrefs.edit(); editor.putBoolean("userMessage", isChecked); editor.commit(); //System.out.println(examplePrefs.getBoolean("userMessage", isChecked)); isDingChecked = examplePrefs.getBoolean("userMessage", isChecked); System.out.println(isDingChecked + " is ding checked"); ding.setChecked(isDingChecked); } }); countdown = (Switch) findViewById(R.id.switch2); countdown.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // do something, the isChecked will be // true if the switch is in the On position isCountdownChecked = isChecked; } }); } } 

I can use booleans in my other activities, so SharedPreference works fine. However, when I return to my activity in the menu and return to this operation with the parameters, the state of the switches will return to the default values, which are true, regardless of what the user indicates. Anyway, can I fix it?

 ding.setChecked(isDingChecked) 

Actually, I am not doing anything. I know that in the past I asked a question similar to this one, it’s just that I didn’t have much activity, and I’ve been dealing with this problem for a long time. Thanks!

+5
source share
2 answers

Try something like this:

  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_options); ding = (Switch) findViewById(R.id.switch1); //grab prefs first final SharedPreferences examplePrefs = getSharedPreferences(PREFS,0); final Editor editor = examplePrefs.edit(); ding.setChecked(examplePrefs.getBoolean("your_key", false)); //false default ding.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //commit prefs on change editor.putBoolean("your_key", isChecked); editor.commit(); System.out.println(isDingChecked + " is ding checked"); } }); 
+16
source

The setChecked(value) method setChecked(value) fine, but you call it inside the onCheckedChanged(...) method, which is not needed.
Therefore, to set the Switch to the last value, you must load the settings and set the checked state outside the setOnCheckedChangeListener listener.

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_options); // your current code //load and set preferences SharedPreferences examplePrefs = getSharedPreferences(PREFS,0); isDingChecked = examplePrefs.getBoolean("userMessage", isChecked); ding.setChecked(isDingChecked); } 
0
source

Source: https://habr.com/ru/post/1203023/


All Articles