CompositeClickListener maintains a list of View.OnClickListeners and when it receives a click event it iterates through the list backward, delegating the click to each in turn. The problem with this approach is that in the case of AbsMaterialCheckablePreference the preference sets itself as a listener in its onViewCreated method, which is the listener responsible for switching the value. Because this is the first listener in the CompositeClickListener's list, it is the last to be executed, so all other click listeners will be executed using the old value of the preference should they call getValue().
This is particularly notable with MaterialPreferenceScreen#setVisibilityController as it sets a click listener on the controller MaterialPreference which is consistently invoked with the wrong boolean value. The sample app demonstrates this behavior (despite what appears to be an attempt to correct for it by setting showWhenChecked to false when it really wants to be true). When you enter the AppConfigActivity, the visibility of Location will be the opposite of what Automatic Location would indicate (because at this point it has the correct value and is respecting showWhenChecked = false. When you switch the value for the first time, the visibility will not change (as it is still using the old value).
CompositeClickListener
maintains a list ofView.OnClickListener
s and when it receives a click event it iterates through the list backward, delegating the click to each in turn. The problem with this approach is that in the case ofAbsMaterialCheckablePreference
the preference sets itself as a listener in itsonViewCreated
method, which is the listener responsible for switching the value. Because this is the first listener in theCompositeClickListener
's list, it is the last to be executed, so all other click listeners will be executed using the old value of the preference should they callgetValue()
.This is particularly notable with
MaterialPreferenceScreen#setVisibilityController
as it sets a click listener on the controllerMaterialPreference
which is consistently invoked with the wrong boolean value. The sample app demonstrates this behavior (despite what appears to be an attempt to correct for it by settingshowWhenChecked
tofalse
when it really wants to betrue
). When you enter theAppConfigActivity
, the visibility of Location will be the opposite of what Automatic Location would indicate (because at this point it has the correct value and is respectingshowWhenChecked = false
. When you switch the value for the first time, the visibility will not change (as it is still using the old value).Possible solutions include:
controller.getValue()
and!controller.getValue()
at line 73 of MaterialPreferenceScreenCompositeClickListener
forward instead of backwardOnPreferenceChangedListener
and relying on that forsetVisibilityController
(this could be more broadly useful as well)AbsMaterialCheckablePreference
set a compositeCompositeClickListener
which always delegates to theAbsMaterialCheckablePreference
first.