SharedPreferences.onSharedPreferenceChangeListener not being called consistently

前端 未结 8 1093
终归单人心
终归单人心 2020-11-22 06:47

I\'m registering a preference change listener like this (in the onCreate() of my main activity):

SharedPreferences prefs = PreferenceManager.get         


        
8条回答
  •  渐次进展
    2020-11-22 07:21

    Kotlin Code for register SharedPreferenceChangeListener it detect when change will happening on the saved key :

      PreferenceManager.getDefaultSharedPreferences(this)
            .registerOnSharedPreferenceChangeListener { sharedPreferences, key ->
                if(key=="language") {
                    //Do Something 
                }
            }
    

    you can put this code in onStart() , or somewhere else.. *Consider that you must use

     if(key=="YourKey")
    

    or your codes inside "//Do Something " block will be run wrongly for every change that will happening in any other key in sharedPreferences

提交回复
热议问题