First of all, this is nothing new but for someone who just started working with Android, this util must be useful. You can found the Github contains the source code for this post here.
Let wrap-up about the Shared Preference in Android. According to Google, SharedPreferences is:
How to create SharedPreferences?if you have a relatively small collection of key-values that you'd like to save, you should use theSharedPreferences
APIs. ASharedPreferences
object points to a file containing key-value pairs and provides simple methods to read and write them. EachSharedPreferences
file is managed by the framework and can be private or shared.
So, this is traditional way to create and persist data in the app using SharedPreferences.
The annoy is, you have to get SharedPreferences every single time you want to touch the data. And, your have to put correct data type for key-value of the pref. That is actually not a good practise and also waste you effort and time. To solve that problem, we can easily write a class file which act as an ultimate SharedPreferences helper for the app.
Better way to implementation with android-shared-preference-util
As you see, we only need 3 lines of code to create a preference and one line of code to retrieve that preference.
As you see, we only need 3 lines of code to create a preference and one line of code to retrieve that preference.
0 comments:
Post a Comment