There are basically 3 ways to get SharedPreferences.
- PreferenceManager.getDefaultSharedPreferences(applicationContext). This is the default used by PreferenceFragment.
- context.getSharedPreferences("NAME", Context.MODE_PRIVATE). Create your own by specifying a name.
- activity.getPreferences(Context.MODE_PRIVATE). To be used for this activity only.
If I need a global SharedPreferences
per application, I prefer PreferenceManager.getDefaultSharedPreferences(applicationContext)
as it is used by PreferenceFragment
as well (avoid having to maintain 2 SharedPreferences
, or reconfigure PreferenceFragment
)
References: