Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Simple Usage

Edison Wang edited this page May 17, 2016 · 5 revisions

From RxJava: One of the most common operations when dealing with asynchronous tasks on Android is to observe the task's result or outcome on the main UI thread. Using vanilla Android, this would typically be accomplished with an AsyncTask.

Using RxJava You would use an Observable, the problem people often run into is that it couples the caller and the receiver.

Using EventService, you can have a fully decoupled MVC model:

Declare the Action (task)

You will first create an "Action" class that takes in what input variables and output events it emits.

@EventProducer(generated = {
        @Event
})
@Action
@ActionHelper
public class LikePostAction implements Action {

    @Override
    public ActionResult processRequest(EventServiceImpl service, ActionRequest actionRequest, Bundle bundle) {
        //Send the like action to your server here.
        return new LikePostActionEvent();
    }
}
  • Implement processRequest() as needed, EventServiceImpl gives you access to the Service/Context object.
  • @EventClass will generate LikePostActionEvent, the resulting event class.
  • @RequestAction will generate PsLikePostActionAction, the request identifier class.
  • @RequestActionHelper will generate LikePostActionHelper, the request helper class.

Calling the action

   PennStation.requestAction(PsLikePostAction.helper());

This is all you need from the caller's perspective, notice how no hard reference is passed in.

Listening on the events

@EventListener(producers = {
        LikePostAction.class
})
public class SampleActivity extends Activity {

    private final SampleActivityEventListener mListener = new SampleActivityEventListener() {

        @Override
        public void onEventMainThread(LikePostActionEvent event) {
            //Task has completed.
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        PennStation.registerListener(mListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        PennStation.unRegisterListener(mListener);
    }

For each listener that cares about the task result, add the action to @EventListener.

This will add the required event methods to the generated ClassEventListener class.

All the events emitted will be passed back on the main thread by default.

Clone this wiki locally