Ali Bakhtiari
Ali Bakhtiari

Reputation: 35

Shared Preferences differences

1) What is the difference between

PreferenceManager.getDefaultSharedPreferences(context)

and

getSharedPreferences(name, mode)

2) And what does each of them do?

3) And how can I make a setting screen (Activity)?

Upvotes: 1

Views: 48

Answers (1)

hata
hata

Reputation: 12473

1) You can have multiple SharedPreference files (so they are called SharedPreferences). The argument name of method getSharedPreferences(name, mode) specifies the the name of SharedPreference file to handle.

PreferenceManager.getDefaultSharedPreferences(context) returns the default SharedPreference file having default name and mode. Default name is based on your app's package name (as packagename_preferences.xml) and default mode is MODE_PRIVATE.

If you just want to use a single SharedPreferences file, PreferenceManager.getDefaultSharedPreferences(context) is concise to use.

2) With SharedPreferences you can save some key-value data.

3) Your last question: how to make a setting screen? is too wide topic to answer here. However, I suggest that using PreferenceActivity or PreferenceFragment you can manage a SharedPreferences without handling SharedPreferences directly.

Upvotes: 1

Related Questions