0

in Android why we have - Context.getSharedPreferences() and -Activity.getPreferences() but we can change the model in each one between private and multiple so it will be same !! i know there is another function but what is it ?

1 Answers1

1

Always useful to read the documentation first! Furthermore reading the source code really helps a lot at times.

  1. Context.getSharedPreferences(String name, int mode)
    This is the main method. What it does is it fetches the contents of preference file "name", stores and returns it via a singleton.
  2. Activity.getPreferences(int mode)
    As said, this just calls the above, but with specific name which actually is equal to: getLocalClassName()
  3. PreferenceManager.getDefaultSharedPreferences(Context)
    This also calls Number 1 with the name: getPackageName() + "_preferences";

That said you can also supply the first two methods with ones of these modes:

  • MODE_PRIVATE
    The default mode you should be using (also default for Number 3).
  • MODE_MULTI_PROCESS
    Meant to be used if your application has multiple processes, where a singleton is not enough to keep the preferences up-to-date.
  • MODE_WORLD_READABLE Deprecated in API 17
  • MODE_WORLD_WRITEABLE Deprecated in API 17
Simas
  • 43,548
  • 10
  • 88
  • 116