Easy preference handling in Android with Kotlin Extension functions | No BS Tutorials

Welcome to No BS Tutorials. Here you will get “To the Point” tutorials with no BS involved.

Saksham Khurana
Devs Life
Sep 28, 2019

--

Photo by Amador Loureiro on Unsplash (keyword: blocks)

This tutorial will be in Kotlin & assumes you have knowledge of Kotlin Extension functions. You can learn more about extension functions in my other article.

Goal :

Create a simple & efficient way to handle Shared Preference in Android

Getting Started :

  • In an empty kotlin file
  • In your main App file which extends Application

Android Studio will auto-import package directory Prefs was in

  • Now create 2 extension functions

Note: SharedPreference accepts String, Int, Boolean, Float, Long & StringSet

  • You can simply read & write anything from anywhere using

Conclusion :

With few lines of code, in the beginning, we can easily create/read/delete shared preference value anywhere in our code

Wrap Up :

Once again if you wish to know in more detail about this entire tutorial visit my blog.

Thanks a lot for tuning in today, for more tutorials kindly follow this publication.

--

--