Created
March 27, 2016 10:10
-
-
Save pareshchouhan/f5e2ce01d779a6154bdf to your computer and use it in GitHub Desktop.
Using otto : minimal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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