【问题标题】:How to use EventBus between activities in android如何在android中的活动之间使用EventBus
【发布时间】:2018-12-14 10:47:36
【问题描述】:

在我的应用程序中,我有 两个 activities
Activity AActivity B
进入 活动 B 我有 dialog 并且当用户单击此 dialog 的按钮之一时,我希望将方法调用到 活动 A
我写了下面的代码,但是当点击dialog 按钮时没有调用 Activity A 中的方法。

活动 B 对话框代码:

vipDialog = new Dialog(context);
                vipDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                vipDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                vipDialog.setContentView(R.layout.base_dialog);
                //Set cancellable
                vipDialog.setCancelable(false);
                //Init views
                baseDialog_logo = vipDialog.findViewById(R.id.baseDialog_logo);
                baseDialog_title = vipDialog.findViewById(R.id.baseDialog_title);
                baseDialog_desc = vipDialog.findViewById(R.id.baseDialog_desc);
                baseDialog_negativeBtn = vipDialog.findViewById(R.id.baseDialog_negativeBtn);
                baseDialog_positiveBtn = vipDialog.findViewById(R.id.baseDialog_positiveBtn);
                baseDialog_buttonsLay = vipDialog.findViewById(R.id.baseDialog_buttonsLay);
                baseDialog_cancelBtn = vipDialog.findViewById(R.id.baseDialog_cancelBtn);
                //Set weight
                baseDialog_buttonsLay.setWeightSum(2);
                baseDialog_cancelBtn.setVisibility(View.GONE);
                //Set logo
                baseDialog_logo.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.dialog_vip_logo));
                //Set text
                baseDialog_title.setText(getString(R.string.trialTitle));
                baseDialog_desc.setText(getString(R.string.trialDesc));
                baseDialog_positiveBtn.setText(getString(R.string.buyVip));
                baseDialog_negativeBtn.setText(getString(R.string.detailSeeAdv));
                //Set click listeners
                baseDialog_positiveBtn.setOnClickListener(v -> {
                    EventBus.getDefault().post(new BuyPremiumUserEvent(true));
                    finish();
                });
                baseDialog_negativeBtn.setOnClickListener(v -> {
                    loadFullPageAdv(getViewContext(), BuildConfig.fullPageDetailApiKey, TapsellAdRequestOptions.CACHE_TYPE_STREAMED);
                });
                vipDialog.show();

活动 A 代码:

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

}

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

@Subscribe(threadMode = ThreadMode.MAIN)
public void onBuyPremium(final BuyPremiumUserEvent event) {
    clickedOnBuyPremium = event.isClickOnBuyPremium();
    initBazaarUserRegistered();
    Log.e("paymentLog", "Clicked");
}

当点击对话框时,不会在日志中显示Log.e("paymentLog", "Clicked");

我该如何解决?

【问题讨论】:

    标签: java android greenrobot-eventbus-3.0


    【解决方案1】:

    您可能需要使用粘性事件。由于 After Activity A 启动 Activity B 后,它进入后台,不再接收任何事件。

    把这个放在你的Activity A而不是EventBus.getDefault().register(this)

    @Override
    public void onStart() {
      super.onStart();
      EventBus.getDefault().registerSticky(this);
    }
    

    你还需要发布sticky这样的事件

    EventBus.getDefault().postSticky(new BuyPremiumUserEvent(true));
    

    然后在您的订阅者中,像这样添加粘性为真

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
      public void onBuyPremium(final BuyPremiumUserEvent event) {
      clickedOnBuyPremium = event.isClickOnBuyPremium();
      initBazaarUserRegistered();
      Log.e("paymentLog", "Clicked");
    }
    

    这是Sticky Events 的文档

    【讨论】:

    • 谢谢亲爱的,您的回答对我有帮助。但没有找到EventBus.getDefault().registerSticky(this);,我用过这个EventBus.getDefault().register(this)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多