Guest User

Untitled

a guest
Nov 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. // 1. Get a reference to SharedPreferences a you normally would.
  2. val prefs: SharedPreferences
  3.  
  4. // 2. Use the extension functions to create a LiveData object of whatever type you need and observe the result.
  5. val boolPrefLiveData = prefs.booleanLiveData("notification_enabled", false)
  6. boolPrefLiveData.observe(this, { enabled ->
  7. if (enabled != null && enabled) {
  8. sendAnalytics()
  9. }
  10. })
  11.  
  12. // 3. If you want to get the value associated with a particular sharedpref key and you have an object of type LiveDataSharedPreferences.kt,
  13. // just use the following code :
  14. val boolValue = boolPrefLiveData.value
Add Comment
Please, Sign In to add comment