Last active
February 11, 2018 10:15
-
-
Save developernca/7ac8b911b6f08327aac2cfbf9e9cd7e0 to your computer and use it in GitHub Desktop.
Android SharedPreferences Utility Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SharedPrefUtils.java can be used to access android SharedPreferences in easy way. Typically created for android's learners to reduce their | |
code but there is no many difference in getting preferences values. | |
//////////////////////////// | |
To put value in preferences. | |
//////////////////////////// | |
SharedPrefUtils.init(this, "my_pref") | |
.put("int_pref", 1203) | |
.put("float_pref", 1.203f) | |
.put("bool_pref", true) | |
.put("str_pref", "String Pref") | |
.put("long_pref", 100000000000099L) | |
.put("set_pref", setObject) | |
.finish(); | |
//////////////////////////////////////////////////////////////////// | |
To get value in preferences, use static method myPref(contex, name). | |
//////////////////////////////////////////////////////////////////// | |
SharedPrefUtils.myPrfe(context, "my_pref").getFloat("float_pref", 0.0f);// assume context as a Context object. | |
///////////////////////////////// | |
To remove a value in preferences. | |
///////////////////////////////// | |
SharedPrefUtils.init(this, "my_pref").remove(keyToRemove).finish(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment