How to subscribe events in Otto from within a POJO service?

146 Views Asked by At

I want to subscribe for events in a POJO service (outside Activity or Fragment). My use case is simple, upon app start, I want to load content from Internet and show in a GridView. The GridView is inside a Fragment class. From inside Fragment's onResume, I am posting an Otto event and trying to subscribe inside a POJO service:

Fragment class

@Override
public void onResume() {
    super.onResume();
    PopularMoviesApplication.getEventBus().register(this);
    PopularMoviesApplication.getEventBus().post(produceDiscoverMovieEvent());
}
    @Produce
    public DiscoverMovieEvent produceDiscoverMovieEvent() {
        return new  DiscoverMovieEvent("popularity.desc");
    }

DiscoverMovieServiceImpl class

public class DiscoverMovieServiceImpl {


        public DiscoverMovieServiceImpl() {
            PopularMoviesApplication.getEventBus().register(this);
        }

        @Subscribe
        public void getMovies(DiscoverMovieEvent event) {
            ...
}

The getMovies method is never called. What am I doing wrong?

1

There are 1 best solutions below

0
On

I fixed this by instantiating Service Impl within Fragment, as shown below:

service = new DiscoverMovieServiceImpl();