66419

Eventbus onMessageEvent not getting called

I have implemented EventBus in my project but I am not getting all of my events

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = findViewById(R.id.btn); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { EventBus.getDefault().post(new MessageEvent()); EventBus.getDefault().post(new MessageEvent2()); } }); } @Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent(MessageEvent event) { Toast.makeText(this, "MainActivity called", Toast.LENGTH_SHORT).show(); }; @Override public void onStart() { super.onStart(); EventBus.getDefault().register(this); } @Override public void onStop() { super.onStop(); EventBus.getDefault().unregister(this); } }

Here i created 2 event inside onClick(); And this is my AnotherActivity where i have another @Subscribe

public class AnotherActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_another); } @Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent(MessageEvent2 event2) { Toast.makeText(this, "AnotherActivity called", Toast.LENGTH_SHORT).show();//Not getting called }; @Override public void onStart() { super.onStart(); EventBus.getDefault().register(this); } @Override public void onStop() { super.onStop(); EventBus.getDefault().unregister(this); } }

I dont know why my second toast is not getting called, i have done every thing correctly.

What i suspect is the AnotherActivity is not created yet so my event is not called is that is so what is use of EventBus then?

Answer1:

What i suspect is the AnotherActivity is not created yet so my event is not called is that is so

Yes, if the event happened in past and the component(activity) is not active/created then the event will not be received.

what is use of EventBus then?

You can use Sticky Event to listen to past events in newly created activity

so use postSticky

EventBus.getDefault().postSticky(new MessageEvent2());

and add sticky = true in subscribe annotation

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN) public void onMessageEvent(MessageEvent2 event2){ Toast.makeText(this, "AnotherActivity called", Toast.LENGTH_SHORT).show();//Not getting called };

or you can receive them manually

MessageEvent2 msg2 = EventBus.getDefault().getStickyEvent(MessageEvent2.class); // you can also remove it using // EventBus.getDefault().removeStickyEvent(msg2);

or remove it from history as well

MessageEvent2 msg2 = EventBus.getDefault().removeStickyEvent(MessageEvent2.class); if(msg2!=null){//do something}

Recommend

  • Is there any API function to get the battery level in Windows Desktop Apps?
  • Geocoder works in 1.6 but not in 2.2 emulator
  • Android Floating Window
  • Could not re-enable the KeyGaurd once disabled it
  • Relay Error when deleting: RelayMutationQuery: Invalid field name on fat query
  • Add foreach value to Ajax
  • Error inflating class ImageButton
  • Angular transclude in a directive containing a ng-template (generic Confirm Modal)
  • problem with classes not found during PlayPlugin.enhance
  • How to Handle Click on Imageview in Custom Listview with image and Textview?
  • Android: How do you create an EditText field in java class
  • CloseOptionsMenu doesn't work?
  • Unable to resolve Static method?
  • Send data from edittext to listview
  • Creating a Multi-Step Modal Using Jquery
  • Retrofit SocketTimeoutException (and/or http 500 error) on http-POST
  • Jquery Mobile pageLoading() Method how does it work?
  • Simulate click Geckofx vb,net
  • How to test if a URL from an Eclipse bundle is a directory?
  • Android fill_parent issue
  • Control modification in presentation layer
  • Email format validation in mvc3 view
  • Projection media query: browser support and workarounds?
  • Android screen density dpi vs ppi
  • Deselecting radio buttons while keeping the View Model in synch
  • Deleting and Updating values from a cusrsor adapter
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Free memory of cv::Mat loaded using FileStorage API
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Getting Messege Twice Using IMvxMessenger
  • Memory offsets in inline assembly
  • How to stop GridView from loading again when I press back button?
  • Turn off referential integrity in Derby? is it possible?
  • failed to connect to specific WiFi in android programmatically
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?