EventBus icon indicating copy to clipboard operation
EventBus copied to clipboard

How to register eventBus in normal class registries ?

Open abbasnaqdi opened this issue 6 years ago • 6 comments

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 .

abbasnaqdi avatar Feb 16 '18 15:02 abbasnaqdi

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

greenrobot-team avatar Feb 20 '18 14:02 greenrobot-team

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.

abbasnaqdi avatar Feb 20 '18 16:02 abbasnaqdi

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.

abbasnaqdi avatar Feb 20 '18 16:02 abbasnaqdi

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

kbxwn avatar Mar 15 '18 02:03 kbxwn

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 avatar Oct 16 '19 09:10 sagrgpt

@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

JeckChou avatar Oct 16 '19 10:10 JeckChou

maybe u can do this

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

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

@subscribe() public void onMessageEvent1(TestSetupActivity.ReterReadingSavedNotification event) { presenter.xxx(); } }`

tomridder avatar Mar 16 '23 10:03 tomridder

Thanks for the recent addition. But closing this as this has not received relevant updates in a long time.

greenrobot-team avatar Mar 27 '23 12:03 greenrobot-team