Created
December 12, 2012 14:53
-
-
Save anonymous/4268353 to your computer and use it in GitHub Desktop.
Added activity relative support for greenrobot/EventBus
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
/** | |
* @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() { | |
} | |
} |
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
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(); | |
} | |
} |
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 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
Oops, created as anon lol!