Eventbus: How to register eventBus in normal class registries ?

Created on 16 Feb 2018  Â·  6Comments  Â·  Source: greenrobot/EventBus

Hello . I use eventbus inside activity and fragment and service.
But there is a problem. For example, how do I register the eventbus in the ViewHolder of the RecyclerView class to use eventbus?

Or how can I use the event bus MVP architecture in the presenter layer or in the model layer?

Usually these classes do not have onStart or onStop events and so on.

In fact, I've created a chat application and I need to communicate and exchange data if I need to broadcast music using the eventbus between the service and ViewHolder .

All 6 comments

Generally, you only have to unregister if a class can/should die and be garbage collected (EventBus continues to hold a reference otherwise). Or of course if it wants to no longer receive events.

So in some use cases just registering is fine.
-ut

I can register only the Constractor eventBus library, but the problem is that it is no longer possible to find out when the eventBus should be unregistered and can not figure out when the class was destroyed.

I do not want to manipulate this operation with GC because I've sometimes seen GC unregistered too late for the eventBus.

What are your solutions to the fact that when the eventBus is registered, in activity and etc ... is this registration valid in child and subclass ?

Or there should be a solution that can inject the class into EventBus and identify and handle EventBus injected classes.

For example, like a dagger 2 that injects classes into a component and we can use component properties in that class.

Please review these items and add them if possible.

public class TestAty extends Activity {
Presenter presenter;
protected void onCreate() {
presenter = new Presenter();
EventBus.getDefault().register(presenter);
}

protected void onDestroy() {
    super.onDestroy();
    EventBus.getDefault().unregister(presenter);
}

}

may be you can try this, hope it can help

I'm stuck on the same issue.
I have a class that is unaware of the Android activity lifecycle and I want to use Eventbus for a few asynchronous function calls.
Registering the bus on the constructor seems correct.
When do I unregister the bus as the class might be garbage collected from time to time?
Please let me know if anyone has a solution.
@oky2abbas @greenrobot-team

@sagrgpt if your event is one time use, you can unregister it in your event callback. if not, you can unregister it on your class holder

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adhamenaya picture adhamenaya  Â·  3Comments

ANewAnonymous picture ANewAnonymous  Â·  4Comments

lalit3686 picture lalit3686  Â·  9Comments

gsteigert picture gsteigert  Â·  5Comments

veeti picture veeti  Â·  3Comments