Skip to content

Instantly share code, notes, and snippets.

@pareshchouhan
Created March 27, 2016 10:10
Show Gist options
  • Save pareshchouhan/f5e2ce01d779a6154bdf to your computer and use it in GitHub Desktop.
Save pareshchouhan/f5e2ce01d779a6154bdf to your computer and use it in GitHub Desktop.
Using otto : minimal
//Create a file BusProvider to provide singleton BusProivder
public class BusProvider {
private static final Bus bus = new Bus();
public static Bus getInstance() {
return bus;
}
}
//Create another file which extends application
public class ExampleApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//Persistent BusProvider, so GC doesn't remove this.
new BusProivder();
}
}
Now time to create Events in events/ , create a class
LoadReviewsEvent.java
public class LoadReviewsEvent {
public final String data;
public LoadReviewsEvent(String data) {
this.data = data;
}
}
Now in your activitys onPause and onResome methods
@Override
public void onResume() {
super();
BusProvider.getInstance().register(this);
}
@Override
public void onPause() {
BusProvider.getInstance().unregister(this);
super();
}
Subscribe for events
@Subscribe
public void handleReviewEvent(LoadReviewsEvent loadReviews) {
//Load reviews here.
//use loadReviews.data
}
someAsyncTask() {
//when we download data , publish and event using otto
BusProvider.getInstance().publish(new LoadReviewsEvent("This is our data"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment