Here I wanna demonstrate to you guys on how to get started on using GreenRobot's EventBus for Android. This is worked using Android Studio.
To get started, add EventBus into Gradle dependency, inside build.gradle.
dependencies {
compile 'de.greenrobot:eventbus:2.1.0'
}
(versions may vary. You need to check Maven Central for the latest version)
The sample code is right here.
In this sample code, I have demonstrated how to pass an object from the PlaceHolderFragment to the SecondFragment by using sticky events, with a trigger of a Button. No more headaches on whether to convert an object into a Parcelable object or not.
There are a lot of use cases that you can use EventBus for. For example, if you have a three pane layout in a 10 inch tablet, you can probably code two fragments to listen to the same event and the values will pass, which proves the decoupled concept.
But, you have to know how the event flows, if not you might end up in confusion at first. It just needs some getting used to.
Enjoy.
Comments
Post a Comment