onChangedListener can't be accessed by multiple classes at the same time
nedomovnyvlad opened this issue · 3 comments
Hello, I noticed that currently, we can set OnChangedListener only by accessing the listener's property in Preference class. Such implementation doesn't allow us to subscribe to preference changes from multiple places in code because every time you set a listener the previous one will be lost. What do you think about adding a method to allow multiple subscriptions?
I can suggest something like this:
public void addOnSomeEntityChangedListener(OnSomeEntityChangedListener listener) {
// implementation
}
By using such method after any preference was changed you can notify every listener that subscribed to its changes before.
@nedomovnyvlad
Thank you for your suggestion!
It's released on version 1.1.7
.
You can use addOnChangedListener
like below.
userProfile.addNicknameOnChangedListener(
new Preference_UserProfile.NicknameOnChangedListener() {
@Override
public void onChanged(String nickname) {
Toast.makeText(getBaseContext(), "onChanged :" + nickname, Toast.LENGTH_SHORT).show();
}
});
userProfile.addNicknameOnChangedListener(
new Preference_UserProfile.NicknameOnChangedListener() {
@Override
public void onChanged(String nickname) {
Toast.makeText(getBaseContext(), "another onChanged listener:" + nickname, Toast.LENGTH_SHORT).show();
}
});
Thank you!
Oh, great! Thanks for a quick answer! But, what do you think about also adding removeOnSomeEntityChangedListener method which will remove the listener from listeners' list? It will help to avoid memory leaks in a case when a subscriber's lifecycle lasts less than the preference's component lifecycle.
@nedomovnyvlad
It is re-released on version 1.1.7
.
If you already downloaded the version, you should refresh gradle dependencies like below.
rm -rf $HOME/.gradle/caches/
remove a listener
userProfile.removeNicknameOnChangedListener(listener);
clear all listeners
userProfile.clearNicknameOnChangedListeners();
Thank you for your issue!