Created
June 10, 2014 05:22
-
-
Save aaalaniz/f5c41a619634969a7b8b to your computer and use it in GitHub Desktop.
Otto Main Thread Bus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainThreadBus extends Bus { | |
private final Bus mBus; | |
private final Handler mHandler = new Handler(Looper.getMainLooper()); | |
public MainThreadBus(final Bus bus) { | |
if (bus == null) { | |
throw new NullPointerException("bus must not be null"); | |
} | |
mBus = bus; | |
} | |
@Override public void register(Object obj) { | |
mBus.register(obj); | |
} | |
@Override public void unregister(Object obj) { | |
mBus.unregister(obj); | |
} | |
@Override public void post(final Object event) { | |
if (Looper.myLooper() == Looper.getMainLooper()) { | |
mBus.post(event); | |
} else { | |
mHandler.post(new Runnable() { | |
@Override public void run() { | |
mBus.post(event); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment