android - EventBus 发送空类

标签 android greenrobot-eventbus

在我的安卓应用中我使用

org.greenrobot.eventbus.EventBus;

1.我创建事件:

public class NotLoginEvent {
}
  1. 在我的 fragment 中发送这个事件:

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

  2. 在 Activity 中我收到了这个事件:

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

不错。它工作正常。 但如您所见,NotLoginEvent 类是空的。那么发空类好不好?也许有另一种解决方案。我不想创建空类来发送消息。

最佳答案

您正在以正确的方式进行操作。

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

这是来自官方文档,所以我认为你做得很好。

关于android - EventBus 发送空类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52161158/

相关文章:

android - 检测正在从电话/设备管理员中删除的应用程序

android - 自定义 View 中的 EventBus

Android NavigationView onDestroy 未调用

java - 事件总线 : Registering without any @Subscribe annotated method

android - Worklight API 中是否有任何应用程序间通信设施?

android - 为什么android webview比原生android浏览器慢很多?

android - 在 Android 设备中添加我自己的键盘而不是默认键盘作为输入

android - 为什么解析 StackOverflowException 会抛出 OOM?

android - 不同的 sqlite 数据库,用于在 android 应用程序中进行测试和实际使用

android - GreenRobot EventBus 错误 : No subscribers registered for event