Difference between getDefaultSharedPreferences and getSharedPreferences in Android


What is Sharedpreferences in Android ?

SharedPreferences is an interface in the Android framework that allows you to save and retrieve key-value pairs of primitive data types. SharedPreferences is the way in which Android stores user preferences, such as which theme a user has selected, or whether or not sound should be enabled. This data is stored in an XML file, and can be accessed by multiple activities in an app.

Storing Data at Activity Level

While developing an Android application if you want to store / access data at activity level then you can use getPreferences.

The main difference between the two methods is in how they retrieve the SharedPreferences instance i.e we only have to pass fileName parameter in getSharedPreferences but not in getDefaultSharedPreferences.

What is getDefaultSharedPreferences() in Android ?

getDefaultSharedPreferences() retrieves the default SharedPreferences file that belongs to the application package. This means that the file is accessible only within the application and can be used across activities or even across the app.

Storing Data at Application Level (Default)

This is also an Application level Shared Preference. The only difference is that the Default Files are maintained at Application Level with some Default Shared Preference File Name.

Code for Initialization of “ getDefaultSharedPreferences()

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);

What is getSharedPreferences() in Android?

getSharedPreferences() allows you to specify a specific SharedPreferences file by passing in a name parameter. This means that you can have multiple SharedPreferences files in your app, and each one can store different sets of key-value pairs.

The getSharedPreferences() method takes two parameters: the name of the SharedPreferences file, and the mode in which to open the file. The mode determines how the file can be accessed by other applications or processes. In the example above, the mode is set to MODE_PRIVATE, which means that the file can only be accessed by the calling application.

Storing Data at the Application Level using getSharedPreferences()

While developing an Android application sometimes we have to access data in different activities, at that time we have to place shared Preference data outside the activity. Using this technique we have to maintain data in multiple shared preference files.

We can store/access the data in multiple shared Preferences using the “ getSharedPreferences() “ method. In this method we have to pass filename as a parameter.

Code for Initialization of “ getSharedPreferences()

SharedPreferences sharedPref = context.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);

The main difference between the two methods is in how they retrieve the SharedPreferences instance. getDefaultSharedPreferences() retrieves the default SharedPreferences file that belongs to the application package. This means that the file is accessible only within the application and can be used across activities or even across the app.

Various modes of accessing Shared Preferences

Following are some of the modes for accessing Shared Preferences

  • MODE_PRIVATE − As the name itself specifies that the mode is private i.e it maintains security. You can only access the Shared Preferences by calling the application

  • MODE_APPEND − This will concat the new Preferences with already existing Preferences.

  • MODE_WORLD_WRITEABLE − In this mode other applications can write into your Shared Preferences.

  • MODE_WORLD_READABLE −In this mode other applications can read your Shared Preferences.

Difference between getDefaultSharedPreferences() and getSharedPreferences() in Android

getDefaultSharedPreferences() method

getSharedPreferences() method

This method is used to get a SharedPreferences object associated with the package name of the application

This method is used to access the named file with the specified operating mode.

It can be used to get the default shared preference file associated with the activity.

It allows you to customize the file name.

It provides a default file name which is the package name of the application.

It takes two parameters, one is the name of the file and another is the operating mode.

It returns the default shared preference for the application.

It returns the shared preference for the given file name.

Conclusion

In Summary, “getDefaultSharedPreferences()” retrieves the default SharedPreferences file for the entire app, while the “getSharedPreference()” allows you to specify a custom file name and access mode.

Updated on: 09-May-2023

540 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements