13

What is the difference, when I get preferences as:

PreferenceManager.getDefaultSharedPreferences(getBaseContext());

and

getPreferences(Context.MODE_PRIVATE);
gobernador
  • 5,659
  • 3
  • 32
  • 51
LA_
  • 19,823
  • 58
  • 172
  • 308

1 Answers1

24
PreferenceManager.getDefaultSharedPreferences(getBaseContext());

Will provide an access to a preferences file that is global for the whole application package ; any activity can access the preferences (internaly, the xml file holding the preferences will be named your.application.package_preferences.xml).

contextInstance.getPreferences(Context.MODE_PRIVATE);

Will provide preferences only for the contextInstance class: only instances of the context's class can access these preferences (said your package is still your.application.package and you're in your.application.package.foo.MyActivity, internaly the preferences file is foo.MyActivity.xml).

OcuS
  • 5,320
  • 3
  • 36
  • 45
  • Well, you explained it better than me. Thumbs up! – Wroclai Apr 13 '11 at 17:23
  • Thanks, OcuS. Could you please also clarify if I use `.getDefaultSharedPreferences(getBaseContext())`, will other applications be able to edit this value? – LA_ Apr 13 '11 at 18:47
  • No, because `getDefaultSharedPreferences(Context context)` will give you preferences in `Context.MODE_PRIVATE` (according to http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/preference/PreferenceManager.java;h=e44a0907d20916613f02843291deef9483433052;hb=HEAD#l347 ). May be you could try something like what is explained in this thread's last post http://groups.google.com/group/android-developers/browse_thread/thread/6f1a12fffd60a824 . Never tried myself, hope it helps anyway. – OcuS Apr 14 '11 at 10:17