You can make the following system of events. There is a special object with the Dispatch method, which we will later redefine:
public class EventObject implements EventObject { @Override public void Dispatch(Object data) { } }
Add a class that implements the registration system for such objects associated with the EventType events that EventType :
public abstract class AbstractStream { public enum EventType {}; private ListMultimap<Object, EventObject> listeners = ArrayListMultimap.create(); public void addListener(EventObject listener, Object eventType){ listeners.put(eventType, listener); } public void removeListener(EventObject listener, Object eventType){ listeners.remove(eventType, listener); } public void callEvent(Object eventType, Object data) { for (EventObject hl : listeners.get(eventType)) hl.Dispatch(data); } } public class RealStream extends AbstractStream { public enum EventType { SomethingHappens } }
Now it’s enough to subscribe to the SomethingHappened event in the class in which you need to call the method:
public class TestClass implements MyActivatedController { private EventObject dispatchObject; public TestClass() { dispatchObject = new EventObject() { @Override public void Dispatch(Object data) { TestMethod(data); } }; } public void Activate() { SomethingStatic.RealStream.addListener(dispatchObject, RealStream.EventType.SomethingHappens); } public void Deactivate() { SomethingStatic.RealStream.removeListener(dispatchObject, RealStream.EventType.SomethingHappens); } public void TestMethod(Object data) { Gdx.app.log("TestMethod", (String)data)); } }
It remains only to call the callEvent method callEvent
SomethingStatic.RealStream.callEvent( SomethingStatic.RealStream.EventType.SomethingHappens, "Test data");
which leads to a call to TestMethod with the parameter we set.