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();
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!
source share