使用 Otto 时订阅者不会被解雇

2023-12-24

我正在 Android 上尝试 Otto,并尝试将消息从我的 Fragment 发送回 Activity。这是我的代码的基础知识:

我的巴士提供商:

public final class BusProvider {

 private static final Bus mInstance = new Bus();

 private BusProvider() {}

 public static Bus getBusProviderInstance() {
     return mInstance;
 }
}

我的活动有以下代码:

public class MyActivity
        extends BaseActivity {

    // ....

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        BusProvider.getBusProviderInstance().register(this);
        // ....
    }

    @OnClick(R.id.profile_image)
    public void onImageClicked() {

        // ...

        MyFragment fragment = MyFragment.newInstance(choices);
        fragment.show(getFragmentManager(), "myChoices");
    }

    @Subscribe
    public void onButtonChoicePicked(MyFragment.ChoicePickedEvent event) {
        Toast.makeText(this, "reaching here", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStop() {
        super.onStop();
        BusProvider.getBusProviderInstance().unregister(this);
    }

    // ...
}

这些是我的片段中的重要代码:

public class MyFragment
        extends BaseDialogFragment {

        // ...

        @Override
        public View onCreateView(LayoutInflater inflater,
                                 ViewGroup container,
                                 Bundle savedInstanceState) {

            LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_choices,
                                                                  container,
                                                                  false);
            setupDialogButtons(inflater, layout);
            return layout;
        }



    private void setupDialogButtons(LayoutInflater inflater, LinearLayout parentView) {
        ChoiceButtonViewHolder holder;

        holder = new ChoiceButtonViewHolder(inflater, parentView);
        holder.populateContent("First Choice", 1);

        parentView.addView(holder.mChoiceTextView);
    }


    class ChoiceButtonViewHolder {

        @InjectView(R.id.item_dialog_choice_desc) TextView mChoiceTextView;
        private int mPosition;

        ChoiceButtonViewHolder(LayoutInflater inflater, ViewGroup container) {
            TextView mChoiceTextView = (TextView) inflater.inflate(R.layout.item_dialog_choice, container, false);
            ButterKnife.inject(this, mChoiceTextView);
        }

        public void populateContent(String choiceDesc, int position) {
            mChoiceTextView.setText(choiceDesc);
            mPosition = position;
        }

        @OnClick(R.id.item_dialog_choice_desc)
        public void onChoiceClicked() {
            MyFragment.this.mDialog.dismiss();
            BusProvider.getBusProviderInstance().post(new ChoicePickedEvent(1));
        }
    }



    public static class ChoicePickedEvent {
        public int mPositionClicked;
        ChoicePickedEvent(int position) {
            mPositionClicked = position;
        }
    }
}

我没有收到任何错误。但是当我从片段中单击按钮时,事件 onButtonChoicePicked 不会被调用。

难道我做错了什么? 我是否误解了奥托的工作方式? 难道 ButterKnife 和 Otto 的奇怪组合导致它不起作用?


确保您导入的是“com.squareup.otto.Subscribe”而不是“com.google.common.eventbus.Subscribe”

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Otto 时订阅者不会被解雇 的相关文章

  • Android 视图可见性消失

    如果我设置了一个视图可见性 它被夸大了 消失了 它会加快我的 UI 速度吗 它将加快 UI 的实际绘制速度 因为 好吧 您不必再绘制它 但它仍然会膨胀 并且膨胀过程不会更快
  • 为什么ArrayList没有getSize()而不是size()?

    我在一些 JSP 页面中大量使用 ArrayList 我想像这样访问 ArrayList myArrayList size 但由于对象必须符合 JavaBean 标准 其中myArrayList getMyPropertyName is m
  • 查找前 N 个五边形数

    我必须找到第一个N pentagonal numbers 1 从 1 100 并每行显示 10 个 我必须使用getPentagonalNumber int n 方法也是如此 显然这就是它存在的原因 到目前为止 这是我的代码 package
  • 在 Android 上解析 RSS

    我有几个 RSS 源需要为我的应用程序进行解析 我按照这里的优秀教程进行操作 http w2davids wordpress com android rssatom feeds parsing with rome http w2davids
  • 使用java将文件从GCS存储桶传输到SFTP服务器

    我能够从 GCS 存储桶读取文件 但所有库都喜欢jsch将文件传输到 SFTP 服务器时会查找文件路径 而不是内存中的文件 我不想将从GCS读取的文件写入磁盘 如何将内存中的文件传输到SFTP 我假设您想上传内存中的数据 JSch 实际上有
  • SpannableString 中的 URLSpan

    我正在使用这个例子SpannableString http developer android com resources samples ApiDemos src com example android apis text Link ht
  • Android - 有没有办法监听 GPS 是否启用或禁用

    我编写此代码是为了接收位置更新 PendingIntent launchIntent PendingIntent getBroadcast context 5000 intent 0 manager requestLocationUpdat
  • spring-hibernate 花费更多时间的任何原因?

    目前 我正在春季和冬眠期间从事一个项目 我来到这里 获取记录并在 JSP 中显示这些记录需要更多时间 我在各处都保留了时间戳 以查看哪里花费了更多时间 Time HomeController start 2014 07 09 18 58 5
  • 如何修复 java.lang.ClassNotFoundException: org.springframework.boot.configurationprocessor.json.JSONException 错误?

    当我在生产环境中将 Spring Boot 服务作为 Windows 服务运行时 出现以下错误 服务exe的创建者是Jar2exe https www jar2exe com java lang reflect InvocationTarg
  • Spring 4 MVC 和 Websockets - 没有合适的默认 RequestUpgradeStrategy

    我需要 Websockets 在我的应用程序中进行实时更新 所以我找到了这个例子并一步一步地做到了here http raymondhlee wordpress com 2014 01 19 using spring 4 websocket
  • 清单合并失败:需要为 显式指定 android:exported

    我的清单文件有问题 错误消息 清单合并失败 android 需要为 明确指定导出 面向 Android 12 及更高版本的应用需要指定显式值android exported当相应的组件定义了意图过滤器时 有关详细信息 请参阅 https d
  • 在 Exif 中写入 XPKeywords

    我想在 Jpeg 图像中写入 XPKeywords 到目前为止 我正在使用 Sansaleen java api 在 Jpeg 图像中写入 Exif 标签 我能够编写大多数标签 例如主题 评论 作者 评级 但无法编写 Windows XP
  • 从 Spring 启动运行 Java 类

    我使用的是Java8和Spring4 3 1 我有一个 Java Spring 应用程序托管由浏览器和移动应用程序客户端访问的 RESTfult 服务 其次 我编写了一个侦听事件的聊天服务器 socket io 来自客户 该聊天服务器正在从
  • 无法读取使用 DataOutputStream 发送的号码

    这是我的客户端代码 Random rand new Random int n rand nextInt 50 1 DataInputStream dis new DataInputStream socket getInputStream D
  • 当列表中不存在 X 时,从列表中查找大于 X 的值

    我试图从列表中查找大于特定值 在我的情况下已知 的值 Example Given list 1 2 5 10 15 list is sorted 查找大于的值X 7在这种情况下 期望的结果 返回一个包含值的列表 10 15 我尝试使用jav
  • Android BottomNavigationView 与自定义视图作为图标[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我正在尝试 Android 支持库中的新 BottomNavigationView 对于其中一个菜单项 我希望有一个自定义视图 以便我
  • 找不到满足版本限制的“com.google.code.findbugs:jsr305”版本

    当生成签名的 APK 进行发布时 我收到此错误消息 Cannot find a version of com google code findbugs jsr305 that satisfies the version constraint
  • 尝试 Catch 性能 Java

    当捕获异常而不是进行检查时 try catch 需要多长时间 以纳秒为单位 假设消息具有用于查找的 HashMap 类型性能 try timestamp message getLongField MessageField TIMESTAMP
  • StableArrayAdapter 与 ArrayAdapter

    我正在看这个ListView教程 列表视图教程 http www vogella com tutorials AndroidListView article html 我想知道创建自己的更好ArrayAdapter 而不仅仅是使用和Arra
  • 在活动之间共享菜单栏

    我的应用程序上有一个菜单栏 我需要在 5 个活动之间共享该菜单栏 我的菜单栏 5 个允许在活动之间切换的按钮 对于任何活动具有完全相同的 UI 和相同的行为 因此我想共享菜单栏 XML 视图代码和控制器代码 我已经找到了一种使用共享 XML

随机推荐