0

I am using Android Shared Preference for saving some small Boolean data. Now if I use the same keyword string that passed as String Parameter to the getSharedPreferences() method to save my all boolean values like that sample code is they conflict?

I mean if I use A common String for all getSharedPreferences() method and inside them if I use different String in putBoolean() is they are saved properly? Actually whats the functionality of the String argument of getSharedPreferences()?

I used this to save values:

public void saveStatus(boolean b){
        SharedPreferences preferences = getApplicationContext().getSharedPreferences("STATUS", android.content.Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("s1",b);
        editor.commit();
    }
Tonmoy
  • 13
  • 2

2 Answers2

0

they won't conflict if you use different key values like s1 s2 s3 different keyword for each boolean value other wise it will overwrite earlier value(This way will make keys unique):

why don't you pass two parameters to tackle issue like this:

public void saveStatus(String key,boolean b){
    SharedPreferences preferences = getApplicationContext().getSharedPreferences("STATUS", android.content.Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putBoolean(key,b);
    editor.commit();
}
NavinRaj Pandey
  • 1,674
  • 2
  • 26
  • 40
0

They will not conflict

Shared Preferences are stored as xml files.So the string argument(first one) you are passing in the getSharedPreferences() is the name of the xml file which will be created in persistent storage while the one you are using in the putBoolean() is the name of the property with its value specified as second argument which wil be added in that xml file.


For further details also see this:

  1. Where are shared preferences stored?
  2. About XML files
Community
  • 1
  • 1
nobalG
  • 4,544
  • 3
  • 34
  • 72
  • Thank you. So that is the fact! Now I am clear about it. I think saving all same type value in one xml should be better than using different xmls that I've used in pervious – Tonmoy Sep 11 '14 at 04:40
  • Yes, its upto your choices and your requirements. – nobalG Sep 11 '14 at 04:42
  • Also see this http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – nobalG Sep 11 '14 at 04:47