How to use Event bus library in android

房东的猫 提交于 2019-12-25 04:24:33

问题


I'm try to use event bus lib, I could not understand it's functionality and see multiple examples. Is it use only services class or use activity and fragment then is use activity what situation we use event bus in activity or fragment.


回答1:


One of the advantages of events is that you can pass objects around to 'somewhere', and you don't need to know where it is picked up. This makes it easy to push your activity or fragment state to a controller class, and then use events sent from this controller back to the activity or fragment.

Because of events, the controller doesn't need to know if the receiving end is an activity or a fragment. This makes it very easy to switch one for the other. On the other hand this also makes it easy to put the controller somewhere else. Like first you have it as an instance in the application class, and then you move it to a service.

I wrote an article with a very concrete example on how you can use events to deal with the Android life cycle as described above: https://medium.com/@JuliusHuijnk/beating-the-android-life-cycle-d00a2f3ed88




回答2:


You can consider EventBus as a lightweight communication channel for passing data within activities, or services, or fragments or between any of them.

Think of EventBus as an underlying layer in your app which is independent of any active activities or services or fragments and their lifecycle.

The main concept on which an EventBus works is that you subscribe to events in either an activity or fragment or service or any components like that and whenever the EventBus has a specific event of the type you subscribed, it notifies your subscribed method in that component and you can perform any task there based on the event that you have received.

Triggering an event is easy and you can do it from any area of your app by just passing a specific event (which is basically a POJO class, let's say MyEvent) to the EventBus, and the bus will handle the rest and correctly deliver it to the appropriate receiver/s.

I would recommend you to try out EventBus 3 from GreenRobot, and go through their documentation here to incorporate their library n your code. I have been using the same without any problems.

EventBus 3 by GreenRobot

Hope this helps.




回答3:


Instead of interface we can simply use EventBus .we can pass messages from one class to one or more classes. EventBus in 3 steps

  • Define
  • Register and unregister
  • Post Event

Define events:

public static class MessageEvent { /* your getter and setter */   }

Prepare subscribers: Declare and annotate your subscribing method, optionally specify a thread mode:

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(MessageEvent event) {
   /* event fire here when you post event from other class or fragment */
  };

Register and unregister your subscriber. For example on Android, activities and fragments should usually register according to their life cycle:

@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);//Register 
 }

unRegister

@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);//unregister
}

Post events:

EventBus.getDefault().post(new MessageEvent());//post event



回答4:


Mostly you shouldn't. In general its a hack for when you misarchitected your program and can't easily pass data from one point to another due to how your app is encapsulated and what objects are known at what levels. It can cause real spaghetti code where its difficult to figure out what code will actually be called when an event occurs. You shouldn't be writing your code around having an event bus, it should be a last resort if you can't refactor things to work the correct way.



来源:https://stackoverflow.com/questions/40987704/how-to-use-event-bus-library-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!