Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifecycle Awareness #628

Open
ueen opened this issue Jan 13, 2020 · 2 comments
Open

Lifecycle Awareness #628

ueen opened this issue Jan 13, 2020 · 2 comments

Comments

@ueen
Copy link

ueen commented Jan 13, 2020

Whats about the registering and unregistering, why cann't the @subscribe function be Lifecycle Aware, registering on Class creation and unregistering on destruction, would be really neat if this could be added :)

@greenrobot-team
Copy link
Collaborator

I guess you try writing your own LifecycleObserver.
https://developer.android.com/topic/libraries/architecture/lifecycle#lc

public class EventBusLifecycleObserver implements LifecycleObserver {
private final LifecycleOwner lifecycleOwner;
private final EventBus eventBus;
public EventBusLifecycleObserver(LifecycleOwner lifecycleOwner, EventBus eventBus) {
this.lifecycleOwner = lifecycleOwner;
this.eventBus = eventBus;
}
@OnLifecycleEvent(Event.ON_START)
void start() {
eventBus.register(lifecycleOwner);
}
@OnLifecycleEvent(Event.ON_STOP)
void stop() {
eventBus.unregister(lifecycleOwner);
}
}

@ueen
Copy link
Author

ueen commented Jan 28, 2020

Yea i resorted to LiveData, as I need to send an event from a background thread even if the app is not opend but after reopening the UI has to react to that event, I couldn't really achieve that with EventBus. LiveData isn't the cleanest solution for events, but it is extremely reliable. In my opinion this behavior would also greatly improve EventBus :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants