Skip to content

Instantly share code, notes, and snippets.

@mustafa01ali
Created December 4, 2014 21:40
Show Gist options
  • Save mustafa01ali/3d03d1be131a806ce561 to your computer and use it in GitHub Desktop.
Save mustafa01ali/3d03d1be131a806ce561 to your computer and use it in GitHub Desktop.
Otto wrapper
import android.os.Handler;
import android.os.Looper;
import com.squareup.otto.Bus;
/**
* @author Mustafa Ali
*/
public class AppBus {
private Bus mBus = new Bus();
private final Handler mainThread = new Handler(Looper.getMainLooper());
/**
* Registers the instance on the bus
*/
public void register(Object object) {
mBus.register(object);
}
/**
* Unregisters the instance
*/
public void unregister(Object object) {
mBus.unregister(object);
}
/**
* Post the event on the bus. All events posted with this method are always delivered on the main thread.
*/
public void post(final Object event) {
if (Looper.myLooper() == Looper.getMainLooper()) {
mBus.post(event);
} else {
mainThread.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