Why the toogleButton get in its default mode?
In my application there are two tabs like SettingTab And ApplicationTab. Now in setting tab i have toogleButton. I am storing the tooglebutton selection in sharedpreference with this code:
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
prefsEditor = myPrefs.edit();
toogleForFullResult.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (((ToggleButton)v).isChecked())
{
Toast.makeText(getApplicationContext(), "Full Result Sound is On", Toast.LENGTH_SHORT).show();
prefsEditor.putBoolean("FullResultIsOn", true);
}
else
{
Toast.makeText(getApplicationContext(), "Full Result Sound is Off", Toast.LENGTH_SHORT).show();
prefsEditor.putBoolean("FullResultIsOn", false);
}
}
});
prefsEditor.commit();
And in ApplicationTab, based on the selection of the toogleButton i am playing some sound. The code is like:
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
fullResultSound = myPrefs.getBoolean("FullResultIsOn", false);
lessResultS开发者_开发问答ound = myPrefs.getBoolean("LessResultIsOn", false);
if(fullResultSound)
{
SettingsTab.toogleForFullResult.isChecked();
}
if(fullResultSound)
{
playSound(soundFileForFullResult);
}
else
{
playSound();
}
Now the problem is, if i select the toogleon then it wirks only for once. And thne if i again start the activity, then the toggle is off. Why i am not able to store the toogle selection in to the sharedPreference ? Please help me.. Thanks.
You need to keep the
prefsEditor.commit();
inside the onclick listener so that the changes are saved. Right now it is outside.
精彩评论