-
-
Save lucassales2/de467a4741353b8c9ca94ea93c8bbfd8 to your computer and use it in GitHub Desktop.
Creates LiveData objects that observe a value in SharedPreferences while they have active listeners.
This file contains 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
// 1. Get a reference to SharedPreferences however you normally would. | |
val prefs: SharedPreferences | |
// 2. Use the extension functions to create a LiveData object of whatever type you need and observe the result. | |
prefs.liveData("analytics_enabled", false).observe(this, { enabled -> | |
if (enabled != null && enabled) { | |
sendAnalytics() | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment