Mahdi.Pishguy Mahdi.Pishguy - 5 months ago 11
Android Question

Android EventBus use singletone class to handle all events

in this below class i try to handle all events on application. i dont like to write some class to handle theme, in EventBus documentation, that use class constructor, my class must be singleton to be has public, then i wrote simple class as :

public class SignalEvents {
private boolean internetConnectionState;
private boolean activityMarketDetailState;

public boolean isInternetConnectionState() {
return internetConnectionState;
}

public void setInternetConnectionState(boolean internetConnectionState) {
this.internetConnectionState = internetConnectionState;
}

public boolean isActivityMarketDetailState() {
return activityMarketDetailState;
}

public void setActivityMarketDetailState(boolean activityMarketDetailState) {
this.activityMarketDetailState = activityMarketDetailState;
}
}


now, for eventBus and send event i try to use:

SignalEvents signal = new SignalEvents();
EventBus.getDefault().post(signal.setActivityMarketDetailState(true));


but then i get error :

Error:(98, 67) error: 'void' type not allowed here

Answer

You need to post objects. For example:

EventBus.getDefault().post(new ActivityMarketDetailStateChanged(true));

And have such a class:

public class ActivityMarketDetailStateChanged {
    public final boolean newState;

    public ActivityMarketDetailStateChanged(boolean newState) {
        this.newState = newState;
    }
}

And then register to subscribe to those events, depending on if you are using EventBus 2.x or 3.x, assuming version 3:

EventBus.getDefault().register(signal);

Need a subscribe method to receive it in your SignalEvents class:

@Subscribe
public void onEvent(ActivityMarketDetailStateChanged event) {
    setActivityMarketDetailState(event.newState);
}