android - What's the difference between getDefaultSharedPreferences() and getPreferences()? -


i'm taking "developing android apps" udacity course. in "lesson 3: new activities , intents > use sharedpreferences" segment, instructor asked me dig around android developer site how user preferences sharedpreferences. however, found different between official documentation , course's solution.

the udacity course's solution says, grab sharedpreferences instance preferenceactivity, should call:

sharedpreferences prefs = preferencemanager.getdefaultsharedpreferences(getactivity()); 

(where getactivity() context here because it's called inside fragment.)

while the official documentation on android developer site indicates should call:

sharedpreferences prefs = getactivity().getpreferences(context.mode_private) 

so what's difference between preferencemanager.getdefaultsharedpreferences(context context) , activity.getpreferences(int mode)?

please note: question not involve getsharedpreferences() requires file name. it's difference between getpreferences() , getdefaultsharedpreferences().

thanks in advance.

acoording link provided android documentation

getsharedpreferences() - use if need multiple preferences files identified name, specify first parameter.

getpreferences() - use if need 1 preferences file activity. because preferences file activity, don't supply name.

so is, use getsharedpreferences when data want save/retrieve can used different activities in app. if preferences used in 1 activity, can use getpreferences.

edit: note said in post linked 'getdefaultsharedpreferences use default name "com.example.something_preferences", getsharedpreferences require name'


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -