diff --git a/EventBus/src/org/greenrobot/eventbus/EventBus.java b/EventBus/src/org/greenrobot/eventbus/EventBus.java index 147bb5ff..e8683103 100644 --- a/EventBus/src/org/greenrobot/eventbus/EventBus.java +++ b/EventBus/src/org/greenrobot/eventbus/EventBus.java @@ -18,6 +18,7 @@ import org.greenrobot.eventbus.android.AndroidDependenciesDetector; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -148,6 +149,7 @@ public void register(Object subscriber) { Class subscriberClass = subscriber.getClass(); List subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass); + Collections.sort(subscriberMethods); synchronized (this) { for (SubscriberMethod subscriberMethod : subscriberMethods) { subscribe(subscriber, subscriberMethod); diff --git a/EventBus/src/org/greenrobot/eventbus/SubscriberMethod.java b/EventBus/src/org/greenrobot/eventbus/SubscriberMethod.java index 1d78d479..699e49f0 100644 --- a/EventBus/src/org/greenrobot/eventbus/SubscriberMethod.java +++ b/EventBus/src/org/greenrobot/eventbus/SubscriberMethod.java @@ -18,7 +18,7 @@ import java.lang.reflect.Method; /** Used internally by EventBus and generated subscriber indexes. */ -public class SubscriberMethod { +public class SubscriberMethod implements Comparable{ final Method method; final ThreadMode threadMode; final Class eventType; @@ -65,4 +65,9 @@ private synchronized void checkMethodString() { public int hashCode() { return method.hashCode(); } + + @Override + public int compareTo(SubscriberMethod subscriberMethod) { + return Integer.compare(subscriberMethod.priority,this.priority); + } } \ No newline at end of file diff --git a/EventBusTestJava/src/main/java/org/greenrobot/eventbus/EventBusStickyEventTest.java b/EventBusTestJava/src/main/java/org/greenrobot/eventbus/EventBusStickyEventTest.java index ec426745..9adda2b5 100644 --- a/EventBusTestJava/src/main/java/org/greenrobot/eventbus/EventBusStickyEventTest.java +++ b/EventBusTestJava/src/main/java/org/greenrobot/eventbus/EventBusStickyEventTest.java @@ -24,6 +24,8 @@ */ public class EventBusStickyEventTest extends AbstractEventBusTest { + private int a = 0; + @Test public void testPostSticky() throws InterruptedException { eventBus.postSticky("Sticky"); @@ -56,6 +58,14 @@ public void testPostStickyTwoSubscribers() throws InterruptedException { assertEquals(6, eventCount.intValue()); } + @Test + public void testPostTwoDiffPriorityStickyEventOrder() throws InterruptedException { + eventBus.postSticky("Sticky"); + eventBus.postSticky(1); + eventBus.register(new TwoDiffPriorityStickyEventSubscriber()); + assertEquals(1, a); + } + @Test public void testPostStickyRegisterNonSticky() throws InterruptedException { eventBus.postSticky("Sticky"); @@ -187,6 +197,19 @@ public void onEvent(IntTestEvent event) { } } + public class TwoDiffPriorityStickyEventSubscriber { + + @Subscribe(sticky = true,priority = 1) + public void onEvent(String event) { + a = 1; + } + + @Subscribe(sticky = true,priority = 2) + public void onEvent(Integer event) { + a = 2; + } + } + public class StickyIntTestSubscriber { @Subscribe(sticky = true) public void onEvent(IntTestEvent event) {