gpt4 book ai didi

java - 如何在android中的 Activity 之间使用EventBus

转载 作者:行者123 更新时间:2023-11-30 05:10:53 27 4
gpt4 key购买 nike

在我的申请中我有两个 activities .
Activity A Activity B
进入 Activity B我有dialog当用户单击其中一个按钮时 dialog ,我想调用方法到 Activity A
我写了下面的代码,但是当点击 dialog 时按钮未调用 Activity A 中的方法。

Activity 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();

Activity 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");进入日志。

我该如何解决?

最佳答案

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

将其放入您的 Activity A 而不是 EventBus.getDefault().register(this)

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

而且您还需要像这样发布粘性事件

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

然后在您的订阅者中,像这样添加 sticky as true

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

这是 Sticky Events 的文档

关于java - 如何在android中的 Activity 之间使用EventBus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53778205/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com