I have a SettingsFragment which extends PreferenceFragmentCompat and the preference.xml
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = super.onCreateView(inflater, container, savedInstanceState)
preferenceManager.findPreference<Preference>(USER_CLOUD_PROXY)?.let {
it.setOnPreferenceChangeListener { _, _ ->
// mainViewModel.resetConnection()
true
}
}
return view
}
}
and xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="User settings">
<SwitchPreference
android:defaultValue="true"
android:enabled="true"
android:key="user_cloud_proxy"
android:title="Use cloud proxy" />
</PreferenceCategory>
</PreferenceScreen>
The switch button indicates with which URL my app has to connect to a webSocket server. So when the user changes the switch state i want to reset the connection through a function. Later in the function i read the sharedPreference value to choose the URL.
However, the it.setOnPreferenceChangeListener
get called before the internal state has changed, which means that i dont read the newValue, but the old.
Is there a callback that gets called AFTER the internal value has changed?
There is OnPreferenceChangeInternalListener.onPreferenceChange
which is Called when this Preference has changed, but this callback is for internal usage only.
So yes, there is no other callback that gets called after the internal value has changed.
OnPreferenceChangeListener.onPreferenceChange
is the only one you can use.
One thing you can do, if you don't want to call the function directly from the callback taking the newValue from it, then you can save the new value in SharedPref manually and then call the function which can use your logic of reading the value from sharedpref and proceeding further.