How to use EventBus between activities in android

1.8k Views Asked by At

In my application i have two activities.
Activity A and Activity B.
into activity B i have dialog and when users click on one of buttons this dialog, i want call method into Activity A
I write below codes, but when click on dialog button not call method in Activity A.

Activity B dialog codes :

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 codes :

@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");
}

When click on dialog, not show me Log.e("paymentLog", "Clicked"); into log.

How can i fix it?

1

There are 1 best solutions below

1
On BEST ANSWER

You probably need to use sticky events. Since After Activity A starts Activity B it goes to the background, and can no longer receive any events.

Put this in your Activity A instead of EventBus.getDefault().register(this)

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

And you also need to post sticky events like this

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

And in your Subscriber, add sticky as true like this

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

Here are the docs for Sticky Events