1

I'm developing an Android app which uses a high amout of SharedPreferences. I have several SharedPreferences files and I'm making up to 77 calls to these files throughout my app. Sometimes I'm using :

static final String FileName= "SharedPreferencesFile";

at the beginning of my activities and then :

SharedPreferences settings = getSharedPreferences(FileName, Context.MODE_PRIVATE);

whenever I need to use them. Some other times I'm just refering to the files directly as in:

 SharedPreferences settings = getSharedPreferences("SharedPreferencesFile", Context.MODE_PRIVATE);

I'm trying to organize things now so I would like to know about different alternatives to do it. My questions are:

  1. Should I define a single "SharedPreferencesFile" for all the variables in my app or using multiple files as I'm doing now is ok?
  2. Should I define all these String FileName= "SharedPreferencesFile" in the strings.xml from my app resources folder instead of putting them at the beginning of my activities and the just use them as SharedPreferences settings = getSharedPreferences(R.string.SharedPreferencesFile, Context.MODE_PRIVATE);
  3. Should I create a helper class that handles all shared preferences calls for every activity as suggested in Android Shared Preferences
Community
  • 1
  • 1
VMMF
  • 906
  • 1
  • 17
  • 28
  • well why would you reference a shared preference with a static variable key then not in other places? keep it consistent and use the static key. Other than that is all preference – tyczj May 18 '16 at 15:53
  • One point to note SharedPreferences will not work reliably across processes (MODE_MULTI_PROCESS is now deprecated as well) - if you're only using one process then you are fine. Personally I always use a helper library I created that uses a content provider (which is process safe) to access my shared preferences, that way if later I decide to add a Service (or something similar) that runs in it's own process and need to access preferences then I'm covered.. – Mark May 18 '16 at 16:21

3 Answers3

2

Should I define a single "SharedPreferencesFile" for all the variables in my app or using multiple files as I'm doing now is ok?

If you can categorize them in a logical fashion do it. Dont just do it for the sake of having them split over multiple files in a random fashion. It will lead to a lot of confusion later

Should I define all these String FileName= "SharedPreferencesFile" in the strings.xml from my app resources folder instead of putting them at the beginning of my activities and the just use them as SharedPreferences settings = getSharedPreferences(R.string.SharedPreferencesFile, Context.MODE_PRIVATE);

It is always good to have string resources extracted to strings.xml. Be careful if you are adding translations and make sure you don't translate the names. Mark them as notranslate string resources.

Should I create a helper class that handles all shared preferences calls for every activity as suggested in Android Shared Preferences

There are already several available. Here is one that I created to simplify the use of SharedPreferences. It is open source and can be used simply by adding a gradle dependency - Android-SharedPreferences-Helper

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
  • @ AndroidMechanic what do you mean by "If you can categorize them in a logical fashion do it" having one file or many? – VMMF May 18 '16 at 17:00
  • 1
    if you can logically group them into categories go for multiple files. otherwise you will end up having a lot of confusion later about which preference value went to which file. – Viral Patel May 18 '16 at 17:10
1

Create single singleton class which will handle ALL your preferences, ideally you should have a getter and setter for each preference and you will only do:

Preferences.getInstance().getSomePref();

and

Preferences.getInstance().setSomePref(value);
Eury Pérez Beltré
  • 2,017
  • 20
  • 28
1

You can always use the same SharedPreferences for every call

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

It will be easy enough to build a wrapper helper class with String get(String key) and void set(String key) around that.

Something like

class MyPrefs {

  static MyPrefs instance;

  static synchronized getInstance(Context ctx) {
     if(instance == null) {
        instance = PreferenceManager.getDefaultharedPreferences(ctx);
     }
     return instance;

  }

  //then getters and setters e.g.
  int getInt(String key, int defVal) {
     return Integer.valueOf(getString(key))
  }

  String get(String key) {
    return instance.getString(key);
  }
}
Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158