在我的应用程序中,我有 2 activities
。
活动A 和活动B 。
进入活动B ,我有dialog
,并且当用户点击此dialog
的按钮之一时,我希望将调用方法插入活动A
我编写了以下代码,但是在活动A 中单击dialog
按钮时未调用方法。
活动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");
。
我该如何解决?
答案 0 :(得分:0)
您可能需要使用粘性事件。自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添加为true
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onBuyPremium(final BuyPremiumUserEvent event) {
clickedOnBuyPremium = event.isClickOnBuyPremium();
initBazaarUserRegistered();
Log.e("paymentLog", "Clicked");
}
的文档