Skip to content

Instantly share code, notes, and snippets.

@amirul12
Created June 23, 2020 14:51
Show Gist options
  • Save amirul12/ec9e9c89a3d09390ca24766947e7c6ef to your computer and use it in GitHub Desktop.
Save amirul12/ec9e9c89a3d09390ca24766947e7c6ef to your computer and use it in GitHub Desktop.
public class CustomListenerService {
public interface MyCustomListener{
public void onSetText(String text);
}
private List<MyCustomListener> myCustomListeners = new ArrayList<>();
public static volatile CustomListenerService customListenerService;
private CustomListenerService(){
if (customListenerService != null){
throw new RuntimeException("use getInstance() to get the sigle instance of this class");
}
}
public static CustomListenerService getInstance(){
if (customListenerService == null){
synchronized (CustomListenerService.class){
customListenerService = new CustomListenerService();
}
}
return customListenerService;
}
public void addCustomListener(MyCustomListener customListener){
this.myCustomListeners.add(customListener);
}
public void removeCustomListener(MyCustomListener listener){
this.myCustomListeners.remove(listener);
}
private void updateViewOnCustomEventDone(String text){
for (int i = 0; i < myCustomListeners.size(); i++ ){
this.myCustomListeners.get(i).onSetText(text);
}
}
public void changeValue(String text){
if (text != null){
updateViewOnCustomEventDone(text);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment