I am using a toggle button in my applications settings page. The problem is, that once I turn the toggle button on
and exit from the settings page and then again go to settings, it shows that toggle button is in off
state. I want it in the on
state until the user turns it off
, even if the app is closed... Is it possible?
Asked
Active
Viewed 114 times
0

ConcurrentHashMap
- 4,998
- 7
- 44
- 53

ammukuttylive
- 365
- 2
- 6
- 15
-
Use shared prefrence and pass statte of it to the shared prefrence – Usman Kurd Feb 04 '13 at 09:01
2 Answers
1
You should use SharedPreferences for that with an onCheckedChangedListener
.
Here's an example how I'm handling it for a CheckBox (should be equal to a ToggleButton, so it's untested, but should work!):
ToggleButton mToggleButton = (ToggleButton) findViewById(R.id.mToggleButton);
mToggleButton.setChecked(getSharedPreferences(mSharedPreferences, MODE_PRIVATE).getBoolean("dontShowAgain", false));
mToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences settings = getSharedPreferences(mSharedPreferences, MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("dontShowAgain", isChecked);
editor.commit();
}
});

ConcurrentHashMap
- 4,998
- 7
- 44
- 53