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?
I fixed this by instantiating Service Impl within Fragment, as shown below: