Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1. Get a reference to SharedPreferences a 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.
- val boolPrefLiveData = prefs.booleanLiveData("notification_enabled", false)
- boolPrefLiveData.observe(this, { enabled ->
- if (enabled != null && enabled) {
- sendAnalytics()
- }
- })
- // 3. If you want to get the value associated with a particular sharedpref key and you have an object of type LiveDataSharedPreferences.kt,
- // just use the following code :
- val boolValue = boolPrefLiveData.value
Add Comment
Please, Sign In to add comment