Skip to content

Instantly share code, notes, and snippets.

@ryanamaral
Forked from JakeWharton/BaseActivity.java
Last active August 29, 2015 14:09
Show Gist options
  • Save ryanamaral/6f57560610a4b308fdd4 to your computer and use it in GitHub Desktop.
Save ryanamaral/6f57560610a4b308fdd4 to your computer and use it in GitHub Desktop.
package com.squareup.example;
public abstract BaseActivity extends SherlockActivity {
private final ScopedBus scopedBus = new ScopedBus();
protected ScopedBus getBus() {
return scopedBus;
}
@Override public void onPause() {
super.onPause();
bus.paused();
}
@Override public void onResume() {
super.onResume();
bus.resumed();
}
}
package com.squareup.example;
public class ScopedBus {
// See Otto's sample application for how BusProvider works. Any mechanism
// for getting a singleton instance will work.
private final Bus bus = BusProvider.get();
private final Set<Object> objects = new HashSet<Object>();
private boolean active;
public void register(Object obj) {
objects.add(obj);
if (active) {
bus.register(obj);
}
}
public void unregister(Object obj) {
objects.remove(obj);
if (active) {
bus.unregister(obj);
}
}
public void post(Object event) {
bus.post(event);
}
void paused() {
active = false;
for (Object obj : objects) {
bus.unregister(obj);
}
}
void resumed() {
active = true;
for (Object obj : objects) {
bus.register(obj);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment