Good day to all.

From the activation I try to send data to the fragment using the EventBus library.

In the onCreate activation method, I add a fragment

getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, FragmentTest.getInstance()).commit(); 

And immediately after that I send the message:

 EventBus.getDefault().post("Test message"); 

Fragment code:

 @Override public void onStart() { EventBus.getDefault().register(this); super.onStart(); } @Override public void onDestroy() { EventBus.getDefault().unregister(this); super.onDestroy(); } @Subscribe(threadMode = ThreadMode.MAIN) public void onEvent(String message) { Toast.makeText(getActivity().getBaseContext(), message, Toast.LENGTH_LONG).show(); } 

But after that, nothing happens. In the logs the following:

 No subscribers registered for event class java.lang.String No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent 

But if you send a message as follows:

 new Thread(new Runnable() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { EventBus.getDefault().post("Test message"); } }); } }).start(); 

That message will be successfully received in the snippet in the onEvent method. What am I doing wrong and why is this happening?

  • 2
    Perhaps, at the time of how you send the message, the onStart() method has not yet worked. In the second case, due to some delay, onStart() time to work out. - post_zeew
  • Yes, most likely, the way it is. - zimper

1 answer 1

Perhaps, at the time of how you send the message, the onStart() method has not yet worked.

In the second case, due to some delay, onStart() time to work out.