LiveData with shared preferences

前端 未结 7 1559
轮回少年
轮回少年 2020-12-15 05:51

I have a settings screen where I am setting some values. When I set those values it gets saved in shared preferences and these values are needed in my request to the network

7条回答
  •  自闭症患者
    2020-12-15 06:17

    I've create a pure kotlin lib to do this - https://github.com/Jintin/PreferencesExtension

    All we need to do is something like:

    val preferenceLiveData = preference.liveData(MY_KEY)
    
    preferenceLiveData.observe(this) {
        // get update here
    }
    

    And sure the preferenceLiveData can be inside ViewModel and let Activity/Fragment observe it. Check out the example here: Activity, ViewModel

提交回复
热议问题