![]() SharedPreferences is a singleton class means this class will only have a single object throughout the application lifecycle. Both of the function return SharedPreferences object to deal with save and get values from preference file. Google recommends to give name as application package name + preferred name.įor example: if your application package name is then preference could be where is the name of preference file.Īs discussed above, to create shared preference developer needs to call getPreferences(int mode) or getSharedPreferences(String name,int mode) method. Please make sure to follow recommendation by Google while giving preference file name. Important Google Recommend Naming Convention For Preference File: This mode is never used in App by Developer. MODE_WORLD_WRITEABLE – It’s similar to mode world readable but with both kind of accesses i.e read and write. MODE_WORLD_READABLE –If developer creates a shared preference file using mode world readable then it can be read by anyone who knows it’s name, so any other outside application can easily read data of your app. This is the most common mode which is used. MODE_PRIVATE means that when any preference file is created with private mode then it will not be accessible outside of your application. Context.MODE_WORLD_WRITEABLE – read/write to other apps.Context.MODE_WORLD_READABLE – readable to other apps.Context.MODE_PRIVATE – default value (Not accessible outside of your application).There are three types of Mode in Shared Preference: Here modes are declared and static final constant in Context class. 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:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |