LiveData with shared preferences
The following awesome piece of code is LiveData Implementation of SharedPreference. It works perfectly. package com.chargingwatts.chargingalarm.util.preference; import android.arch.lifecycle.LiveData import android.content.SharedPreferences abstract class SharedPreferenceLiveData<T>(val sharedPrefs: SharedPreferences, val key: String, val defValue: T) : LiveData<T>() { private val preferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key -> if (key == this.key) { value = getValueFromPreferences(key, defValue) } } abstract … Read more