

SHARED PREFERENCE MANAGER ANDROID
To create activity based preferences or custom preferences developer have to pass mode to let the system know about privacy of preference file.īefore we begin a brief discussion on ContextĬontext is an abstract class used to get global information in Android Application resources like strings, values, drawables, assets etc. and mode is used to set kind of privacy for file. Here name is any preferred name for example: User,Book etc. Name of the preference file is passed in first parameterĬustom Preferences can be created by calling function getSharedPreferences(String name,int mode), it can be called from anywhere in the application with reference of Context.Used in cases when more than one preference file required in Activity.Developer needs to use getSharedPreferences(String name,int mode) for custom preferences.Important Note: Mode are discussed in Custom preferences. Just because Activity contains only one preference file so getPreferences(int mode) function simply pass the name of Activity class to create a preference file. The function getPreferences(int mode) call the other function used to create custom preferences i.e getSharedPreferences(String name,int mode). To use activity preferences developer have to call function getPreferences (int mode) available in Activity class. They prefer using custom getSharedPreferences(String name,int mode). Developer doesn’t usually prefer using this even if they need only one preference file in Activity.It doesn’t require name as it will be the only preference file for this activity.Use only when one preference file is needed in Activity.For activity preferences developer have to call function getPreferences (int mode) available in Activity class.There are two different ways to save data in Android through Shared Preferences – One is using Activity based preferences and other is creating custom preferences. Two Ways To Save Data Through Shared Preference:
SHARED PREFERENCE MANAGER CODE
8 Code Of Saving & Retrieving Data in Shared Preference:.


