Skip to content

Instantly share code, notes, and snippets.

@chrisjenx
Forked from anonymous/EventBusSupport.java
Created December 12, 2012 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisjenx/4268365 to your computer and use it in GitHub Desktop.
Save chrisjenx/4268365 to your computer and use it in GitHub Desktop.
/**
* @author chris.jenkins
* @created Dec 4, 20124:06:20 PM
*/
package com.example.android.util;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import de.greenrobot.event.EventBus;
/**
* @author chris.jenkins
*/
public class EventBusSupport {
private static final Map<Activity, EventBus> mActivityBus = new HashMap<Activity, EventBus>();
// private static final Map<Activity, List<Object>> mActivityLinks = new HashMap<Activity, List<Object>>();
/**
* Creates a unique event bus for the current activity, useful for fragments to call. Calling {@link #removeActivityBus(Activity)} will remove the
* instance of the bus and call {@link #getActivityBus(Activity)} again will create a new one.
*
*
* @author chris.jenkins
* @param activity
* @return
*/
public synchronized static EventBus getActivityBus(final Activity activity) {
if (activity == null) {
throw new InstantiationError("Activity is null");
}
final EventBus bus = findBus(activity);
return bus;
}
/**
* I would only recommend calling this in onDestroy of the {@link Activity} not in the fragments, I could add registering but for simplicity and
* efficenty its better to just trak the activity object.
*
* @author chris.jenkins
* @param activity
*/
public synchronized static void removeActivityBus(final Activity activity) {
if (activity == null) {
throw new InstantiationError("Activity is null");
}
removeBus(activity);
}
/**
* Will find or create a unique bus for the activity
*
* @author chris.jenkins
* @param activity
* @return
*/
private static EventBus findBus(final Activity activity) {
if (!mActivityBus.containsKey(activity)) {
mActivityBus.put(activity, new EventBus());
}
return mActivityBus.get(activity);
}
private static void removeBus(final Activity activity) {
if (mActivityBus.containsKey(activity)) {
mActivityBus.remove(activity);
}
}
private EventBusSupport() {
}
}
class ExampleActivity {
/**
* Make sure we unregister the bust as to not maintain unessary reference to the activity or the bus
*/
@Override
protected void onDestroy() {
EventBusSupport.removeActivityBus(this);
super.onDestroy();
}
}
public abstract class BaseFragment extends SherlockFragment {
protected EventBus mBus;
@Override
public void onAttach(final Activity activity) {
super.onAttach(activity);
mActionBar = getSherlockActivity().getSupportActionBar();
if (hasContextAwareEventBus()) {
mBus = EventBusSupport.getActivityBus(activity);
} else {
mBus = EventBus.getDefault();
}
}
/**
* Override with true if you want this fragment to use the {@link EventBus} tied to the activity instead of the global one
*
* @author chris.jenkins
* @return
*/
protected boolean hasContextAwareEventBus() {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment