![]() ![]() On the other hand, for storing Boolean, putBooean()/getBoolean() needs to be called innorder to store and retrieve of the preferences respectively. Seems pretty straight forward till the application begins to grow and the number of the preferences which it requires to store increase with it. getString("background_color", defaultValue) Īs it can be seen above for storing the string value in the shared preferences, the putString method needs to be called as well as getString method for retrieving it. getDefaultSharedPreferences(applicationContext) String defaultValue="white" String Value = sp. ![]() Then Android will save it in /data/data/YOUR_APP_PACKAGE_NAME/shared_prefs/YOUR_APP_PACKAGE_NAME_preferences.xml like this green Īnd later the value of the “background_color” can be retrieved from the Shared Preferences like this SharedPreferences sp = PreferenceManager. putString("background_color", "green") editor.apply() getDefaultSharedPreferences(applicationContext) SharedPreferences.Editor editor = sp.edit() editor. Android has offered a simple mechanism to achieve the storage of the user’s preferences by introducing the Shared Preferences library.Ī simple storage of a setting like the background color goes like this SharedPreferences sp = PreferenceManager. while developing an Android application, there are plenty of features or settings which are tightly bounded to the user’s taste, like the colors, themes,fonts,ringtones and etc therefore these preferences need to be saved somewhere to be retrieved on the next execution of the application and that is when the need for a small storage comes to the surface. This article will demonstrate a practical method of abstracting all the possible read and write methods for Shared Preference into a simple generic class.īasically, Shared Preference is a XML file which is acting as a small storage mean for the user’s preferences(settings). If you have landed here, you have been exposed to the hardship of keeping the Shared Preferences of Android inline. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |