EventBus icon indicating copy to clipboard operation
EventBus copied to clipboard

IllegalStateException when a subscriber has three super class who have the same @Subcriber methods

Open ruoyewu opened this issue 5 years ago • 3 comments

the code :

public class EventBusActivityP extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_bus);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 0, sticky = false)
    public void onEventMessage(Message message) {
        Toast.makeText(this, message.msg, Toast.LENGTH_SHORT).show();
    }

    public static class Message {
        String msg;

        public Message(String msg) {
            this.msg = msg;
        }
    }
}

public class EventBusActivityQ extends EventBusActivityP {


    @Subscribe(threadMode = ThreadMode.MAIN, priority = 0, sticky = false)
    public void onEventMessage(Message message) {
        Toast.makeText(this, message.msg, Toast.LENGTH_SHORT).show();
    }
}

public class EventBusActivityR extends EventBusActivityQ {


    @Subscribe(threadMode = ThreadMode.MAIN, priority = 0, sticky = false)
    public void onEventMessage(Message message) {
        Toast.makeText(this, message.msg, Toast.LENGTH_SHORT).show();
    }
}

public class EventBusActivity extends EventBusActivityR {

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 0, sticky = false)
    public void onEventMessage(Message message) {
        Toast.makeText(this, message.msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
}

when a activity has three parents and they have the same @Subcriber method onEventMessage(Message), it will throw IllegalStateException in class SubcriberMethodFinder. I think there is something wrong in method checkAdd() leading to the Exception.

ruoyewu avatar Apr 08 '19 02:04 ruoyewu

Only annotate the method of the child with @Subscribe. In this case EventBusActivity. -Uwe

greenrobot-team avatar Apr 30 '19 05:04 greenrobot-team

i think the much better design here is to have a hierarchy of events as well . one for each activity .

class ActivityEvent extends ActivityQEvent {...} class ActivityQEvent extends ActivityPEvent {...} class ActivityPEvent {...}

then posting an event instance of ActivityEvent.class will notify all three .

SaeedJinat avatar Aug 06 '19 12:08 SaeedJinat

eventbus will find all @subscribe method in register Class and it 's all super class. so all the three onEventMessage will be executed.

tomridder avatar Mar 16 '23 09:03 tomridder