1

In my android app I use

org.greenrobot.eventbus.EventBus;

1.I create event:

public class NotLoginEvent {
}
  1. In my fragment send this event:

    EventBus.getDefault().post(new NotLoginEvent());

  2. And in activity I receive this event:

        @Subscribe()
        public void onNotLoginEvent(NotLoginEvent notLoginEvent) {
           // do something
        }
    

Nice. It's work fine. But as you can see the class NotLoginEvent is empty. So is it good to send empty class? Maybe has another solution. I wan't to create empty class to send message.

Alexei
  • 14,350
  • 37
  • 121
  • 240

2 Answers2

2

You are doing it in the proper way.

public static class MyEvent { /* Additional fields if needed */ }

This is from the official documentation, so i think you are doing it well.

Juanjo Berenguer
  • 789
  • 1
  • 5
  • 8
1

But as you can see the class NotLoginEvent is empty.

It contains no members, but it is not empty. It still represents the event type. Without this class you don't know what the event is or how to listen for it. It's not a problem to create a class just for this purpose.

Additionally, you might add some members to this class in the future. It will be easy to do that because the class already exists.

Tim
  • 41,901
  • 18
  • 127
  • 145