1

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);
    lessResultSound = 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.

Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • 1
    might need to make it writable. myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_WRITABLE); – blessanm86 Sep 10 '11 at 07:52
  • @blessenm i have edited Mode from MODE_WORLD_READABLE to MOCE_WORD_WRITABLE but still it not works. Still the toggle button remain in its default "off" condition even if i have selected it as "on". – Shreyash Mahajan Sep 12 '11 at 04:38
  • http://stackoverflow.com/questions/2566430/sharedpreferences-file this tells about the location of the shared preference file. Try acessing it and see if you values are actually getting stored in the file. – blessanm86 Sep 12 '11 at 04:45
  • Ok. Thanks for the link. Let me check it., – Shreyash Mahajan Sep 12 '11 at 05:24
  • I got that xml file based on the given instruction. but i am not able to see the data in to that file. So now how can i able to see that data ? – Shreyash Mahajan Sep 12 '11 at 05:28

1 Answers1

1

You need to keep the

prefsEditor.commit(); 

inside the onclick listener so that the changes are saved. Right now it is outside.

blessanm86
  • 31,439
  • 14
  • 68
  • 79
  • Ok. I have keep it in to the listener. but still when i close the application or start the another activity in to that application and after wen i see that toogleButton, the state of the button remains as default. Not as we have changed. So what more i nedd to do ? – Shreyash Mahajan Sep 12 '11 at 06:54
  • your code is working for me. You might need to debug on your own by pulling the preference file after each operation to see if you're accidentally doing something. – blessanm86 Sep 12 '11 at 08:42