0

Hello I am writnig an Android app that has two preferences a checkbox and a preferencelist. When the check box is marked as checked the preferencelist becomes enabled. I have manage to save the checkbox "checked" status using putBoolean() method.

getPreferenceManager().getSharedPreferences().edit().putBoolean(key, boolean);
getPreferenceManager().getSharedPreferences().edit().commit();

But how do I save the isEnabled value so that when I leave and return it will not reset?

and how does putboolean knows to whice property to set the boolean anyway?

@Override 
public void onPause() { 
    super.onPause(); 
    save(l.isEnabled());
} 

@Override 
public void onResume() { 
    super.onResume(); 
    l.setEnabled(load());
} 

private void save(final boolean b) { 

          //what to put instead of key in order to save the preference list ENABLED sate?? 
          getPreferenceManager().getSharedPreferences().edit().putBoolean(key, b);
    getPreferenceManager().getSharedPreferences().edit().commit();

}

private boolean load(String key) { 
    return getPreferenceManager().getSharedPreferences().getBoolean(key, false);

} 
reut
  • 21
  • 9
  • What do you mean by 'return'? Are you finishing the activity or just switching between background and foreground? – Hassan Jawed Oct 17 '12 at 13:19
  • More code required to solve this question. – Egor Oct 17 '12 at 13:21
  • finishing... like with putboolean and the commit methods do, only that i want to save the enable state..and how does the putboolean method knows to which status i want to put the boolen, all she gets is the key and the boolean – reut Oct 17 '12 at 13:23

2 Answers2

2

The enabled state can be saved in the same way as you have saved the "checked" status, because isEnabled() returns a boolean.

SharedPreferences.Editor prefEditor = PreferenceManager.getDefaultSharedPreferences(this).edit();
prefEditor.putBoolean("prefs.preferenceList.enabled", preferenceList.isEnabled());  
prefEditor.commit();

To then return the state you want to set the enabled state of the checkbox with setEnabled(). During onCreate you can do something like this.

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
preferenceList.setEnabled(prefs.getBoolean("prefs.preferenceList.enabled", false);
jimmithy
  • 6,360
  • 2
  • 31
  • 29
  • thanxs alot for your reply first of all but its not workings... i'm in a preferencefregment class my preferencelist is called listPref and i do this: getPreferenceManager().getSharedPreferences().edit().putBoolean("listPref.Enabled", b); its not saving it – reut Oct 17 '12 at 13:48
  • Can you show me where the variable 'b' is defined? Also have you been sure to run commit() after putBoolean? – jimmithy Oct 17 '12 at 13:56
  • yes sure private void save(final boolean b) { getPreferenceManager().getSharedPreferences().edit().putBoolean("listPref.enabled", b); } its the same with both check box and list and the check box works fine – reut Oct 17 '12 at 14:28
  • and what calls this function is: public void onPause() { super.onPause(); save("listPref",l.isChecked());} where l is defined to be the ListPreference field – reut Oct 17 '12 at 14:34
  • The save method in the first post takes a single argument, but the save method called in the following post takes two arguments. Do you have a save method defined which takes two arguments? Perhaps you're calling the wrong method? – jimmithy Oct 17 '12 at 17:43
  • oh.. sorry i pased the function before changing.. the bottom line is that the function putboolean gets the string "lisrPref.enabled" and a boolean value and it changes nothing.... – reut Oct 17 '12 at 18:14
  • jimmithy, I think you have a typo there, you're calling `edit()` twice on the same `prefEditor`. The second edit() gets a new instance of the editor (which doesn't have the change), thus the commit does nothing. After `putBoolean` should just be `prefEditor.commit()` @reut – Tim Oct 18 '12 at 16:03
0
getPreferenceManager().getSharedPreferences().edit().putBoolean(key, b);
getPreferenceManager().getSharedPreferences().edit().commit();

You should not be calling edit() a second time, as each time you call edit it creates a new instance of the preferenceEditor. Thus your putBoolean is never committed.

This should be

SharedPreferences.Editor prefs = getPreferenceManager()
                                 .getSharedPreferences()
                                 .edit();
prefs.putBoolean(key,b);
prefs.commit();
Tim
  • 35,413
  • 11
  • 95
  • 121