Shared Preferences is an APIs which serves as a local storage area using key-value. Using this, we can save a flaging or small temporary data easily. For detail about shared preferences, you can read more explanation here.
So here we will create a collection function for setter and getter data. We will create for each primitive data types on android development.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
.... object SPHelper { private var sharedPrefs: SharedPreferences? = null private var editor: SharedPreferences.Editor? = null private val preferenceName = "YourPreferencesName" fun saveBoolean(context: Context?, data: Boolean, key: String?) { sharedPrefs = context?.getSharedPreferences(preferenceName,Context.MODE_PRIVATE) editor = sharedPrefs?.edit() editor?.putBoolean(key,data) editor?.apply() } fun getBoolean(context: Context?, key: String?): Boolean? { sharedPrefs = context?.getSharedPreferences(preferenceName,Context.MODE_PRIVATE) return sharedPrefs?.getBoolean(key, false) } fun saveString(context: Context?, data: String?, key: String?) { sharedPrefs = context?.getSharedPreferences(preferenceName,Context.MODE_PRIVATE) editor = sharedPrefs?.edit() editor?.putString(key, data)?.apply() } fun getString(context: Context?, key: String?): String? { sharedPrefs = context?.getSharedPreferences(preferenceName,Context.MODE_PRIVATE) return sharedPrefs?.getString(key, "") } fun saveInt(context: Context?, data: Int, key: String?) { sharedPrefs = context?.getSharedPreferences(preferenceName,Context.MODE_PRIVATE) editor = sharedPrefs?.edit() editor?.putInt(key, dat)?.apply() } fun getInt(context: Context?, key: String?): Int? { sharedPrefs = context?.getSharedPreferences(preferenceName,Context.MODE_PRIVATE) return sharedPrefs?.getInt(key, 0) } fun saveLong(context: Context?, data: Long, key: String?) { sharedPrefs = context?.getSharedPreferences(preferenceName,Context.MODE_PRIVATE) editor = sharedPrefs?.edit() editor?.putLong(key, dat)?.apply() } fun getLong(context: Context?, key: String?): Long? { sharedPrefs = context?.getSharedPreferences(preferenceName,Context.MODE_PRIVATE) return sharedPrefs?.getLong(key, 0L) } fun saveFloat(context: Context?, data: Float, key: String?) { sharedPrefs = context?.getSharedPreferences(preferenceName,Context.MODE_PRIVATE) editor = sharedPrefs?.edit() editor?.putFloat(key, dat)?.apply() } fun getFloat(context: Context?, key: String?): Float? { sharedPrefs = context?.getSharedPreferences(preferenceName,Context.MODE_PRIVATE) return sharedPrefs?.getFloat(key, 0.0f) } } |
As you can see above, i create an object class called with SPHelper. I also create function for each primitive data type. For each get function, we set default value so if we haven’t save anything before, we will get the default value.
I’ll give an example to save a string data and get them back.
1 2 3 4 5 6 7 |
.... val key = "myword" // Save string data SPHelper.saveString(this, "Hello", key) // fetching string data SPHelper.getString(this, key) |
Thanks for reading this article and happy coding!
Give your comment if you have a hightlight with this article 🙌