Project Url: afollestad/rxkprefs
Introduction: A small, Rx-powered shared preferences library for Kotlin.
More: Author   ReportBugs   OfficialWebsite   


This library provides reactive shared preferences interaction with very little code. It is designed specifically to be used with Kotlin.

Inspiration has been taken from other libraries, but it was written from the ground up on its own.

Download Build Status Codacy Badge License

Gradle Dependency

Add this to your module's build.gradle file:

dependencies {

  implementation 'com.afollestad:rxkprefs:1.2.0'

Getting Started

The core of the library is the RxkPrefs interface. You can retrieve an instance of this interface with the rxkPrefs method, which takes 3 parameters. One of these parameters is optional (the shared preferences mode).

// First parameter is your Context, like an Activity, the second is a key.
val myPrefs = rxkPrefs(this, "my_prefs")

// The optional third parameter is a mode, it defaults to MODE_PRIVATE above.
// This is like using Context.getSharedPreferences("my_prefs", MODE_PRIVATE)
val myPrefs = rxkPrefs(this, "my_prefs", MODE_PRIVATE)

Retrieving a Preference

With a RxkPrefs instance, you can retrieve preferences. By that, I do not mean the raw value of the preference, but an instance of the Pref interface which provides more functionality.

val myPrefs: RxkPrefs = // ...

// Getting a string preference is as simple as this:
val myString: Pref<String> = myPrefs.string("my_string", "default_value")

// You could omit the second parameter to use the default, default value (empty string)
val myString: Pref<String> = myPrefs.string("my_string")

Interacting with a Preference

Once you have a reference to a preference, there are a few things you can do with them.

val myPref: Pref<Int> = // ...

// The key of the preference - first parameter passed in prefs.integer(...) or any other pref getter
// This is always a String.
val key: String = myPref.key()

// The default value of the preference - second parameter passed in prefs.integer(...) or any other pref getter...
// Or the primitive default, such as an empty string, 0, or false.
val defaultValue: Int = myPref.defaultValue()

// The current value of the preference, or the default value if none.
val currentValue: Int = myPref.get()

// Changes the value of the preference.

// True if a value has been set, otherwise false.
val isSet: Boolean = myPref.isSet()

// Deletes any existing value for the preference.

// See "The Preference Observable" below 
val observable: Observable<Int> = myPref.observe()

The Preference Observable

You can receive changes to a preference in real-time with its Observable.

val myPref: Pref<Long> = // ...
val obs = myPref.observe()

val sub = obs.subscribe { newValue ->
  // use new value
sub.dispose() // when you no longer want to receive values

Further usage of this is more of an RxJava issue and less specific to this library. You should have a basic understanding of what you can do with RxJava and what its use cases are.

The Preference Consumer

Pref itself acts as an Rx Consumer. You can use this to save preference values from the emissions of an Observable.

Say you're using RxBinding to bind Android views to Observables that emit when their value changes, such as a CheckBox:

val myPref: Pref<Boolean> = // ...


Whenever the checkbox is checked or unchecked, the underlying boolean shared preference is set to true or false automatically.

Basically, it works like this:

val myObs: Observable<String> = // ...
val myConsumer: Consumer<String> = // ...which can be an instance of Pref

Support Me
About Me
Google+: Trinea trinea
GitHub: Trinea