Instantly share code, notes, and snippets.
idish
/ BooleanSharedPreferenceLiveData.java
Created
January 30, 2019 23:18
This file contains hidden or 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
abstract class SharedPreferenceLiveData<T> extends LiveData<T> { | |
SharedPreferences sharedPrefs; | |
String key; | |
T defValue; | |
public SharedPreferenceLiveData(SharedPreferences prefs, String key, T defValue) { | |
this.sharedPrefs = prefs; | |
this.key = key; | |
this.defValue = defValue; |
rharter
/ SharedPreferenceLiveData.kt
Last active
March 19, 2023 08:15
Creates LiveData objects that observe a value in SharedPreferences while they have active listeners.
This file contains hidden or 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
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) |