A lightweight eventbus library for android, simplifies communication between Activities, Fragments, Threads, Services, etc.
This is an EventBus library for Android. It simplifies the communication between Activities, Fragments, Threads, Services, etc. and lowers the coupling among them to a great extent, thus making simplifier codes, lower coupling possible and improving code quality.
AndroidEventBus is like the Observer Pattern. It will have the objects which need to subscribe events registered into the EventBus through Function “register” and store such subscription methods and subscription objects in Map. When a user posts an event somewhere, the EventBus will find corresponding subscription object in accordance with the parameter type and tag of the Event and then execute the method in subscription object. These subscription methods will be executed in the Thread Mode designated by the user. For example, mode=ThreadMode. ASNYC means the subscription method is executed in the sub-thread. Please refer to the following instructions for more details.
You can use AndroidEventBus according to the following steps.
public class YourActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// register the receiver object
EventBus.getDefault().register(this);
}
@Override
protected void onDestroy() {
// Don’t forget to unregister !!
EventBus.getDefault().unregister(this);
super.onDestroy();
}
}
public class YourActivity extends Activity {
// code ......
// A receiving method with a default tag will execute on UI thread.
@Subscriber
private void updateUser(User user) {
Log.e("", "### update user name = " + user.name);
}
// When there is a “my_tag”, only events designated with “my_tag” can
// trigger the function and execute on UI thread when a user posts an event.
@Subscriber(tag = "my_tag")
private void updateUserWithTag(User user) {
Log.e("", "### update user with my_tag, name = " + user.name);
}
// When there is a “my_tag”, only events designated with “my_tag” can trigger the function.
// The function will execute on the same thread as the one post function is executed on.
@Subscriber(tag = "my_tag", mode=ThreadMode.POST)
private void updateUserWithMode(User user) {
Log.e("", "### update user with my_tag, name = " + user.name);
}
// When there is a “my_tag”, only events designated with “my_tag” can trigger
// the function and execute on an independent thread when a user posts an event.
@Subscriber(tag = "my_tag", mode = ThreadMode.ASYNC)
private void updateUserAsync(User user) {
Log.e("", "### update user async , name = " + user.name + ", thread name = " + Thread.currentThread().getName());
}
}
User class is approximately as follows :
public class User {
String name ;
public User(String aName) {
name = aName ;
}
}
The receiving function will use “tag” to mark receivable types of events, just like designating “action” in BroadcastReceiver, which can deliver messages precisely. Mode can designate which thread the object function will be executed on but defaultly it will be executed on UI thread for the purpose of convenient UI update for users. When the object method executes long-running operations, the “mode” can be set as ASYNC so as to be executed on sub-thread.
EventBus.getDefault().post(new User("android"));
// post a event with tag, the tag is like broadcast's action
EventBus.getDefault().post(new User("mr.simple"), "my_tag");
// post sticky event
EventBus.getDefault().postSticky(new User("sticky"));
After posting the event, the object registered with the event type will receive responsive event.
It will be enough to add the jar file into the “quote” part of the Project, AndroidEventBus.AndroidEventBus.jar
dependencies {
// add AndroidEventBus dependency
compile 'org.simple:androideventbus:1.0.5.1'
}
private void onEventMainThread(User aUser) {
// code
}
If there are two receiving functions of the same parameter type and both are executed on the main thread, how to name them distinctively? Supposing that there are two functions meeting the requirements and the event is adding user, but because the EventBus judges receiving function only by parameter type of the event, both function will be executed. The strategy of AndroidEventBus is to add a “tag” for each event, and use parameter type and “tag” to jointly mark the uniqueness of the vent so as to ensure precise delivery.
These are the differences between AndroidEventBus and EventBus of greenrobot. But it is understandable for greenrobot’s approach considering performance. And what I try to express is that there are very limited quantity of events posted in an App and the performance difference is negligible while user experience is well sensible. What I need to point out is that I know little about the ins and outs of EventsBus of greenrobot and there could be errors among what I’ve mentioned. If that happens, you are more than welcome to correct me.
library | Whether the subscription function can be executed on other thread | features |
---|---|---|
greenrobot's EventBus | yes | It adopts name pattern which is efficient but inconvenient to use. |
square's otto | no | It is convenient to use annotation but it’s not as efficient as EventBus |
AndroidEventBus | yes | It is convenient to use annotation but it’s not as efficient as EventBus. The subscription supports tag (like the Action in Broadcast Receiver) which can make event delivery more accurate and applicable to more usage scenarios. |
-keep class org.simple.** { *; }
-keep interface org.simple.** { *; }
-keepclassmembers class * {
@org.simple.eventbus.Subscriber <methods>;
}
-keepattributes *Annotation*
I really appreciate E-pal “淡蓝色的星期三” for his proposing of bugs and feedback and I hope more and more friends will join our team of AndroidEventBus Development.
Solved the problem of failing to receive an event when the parameter of the subscription method is a basic type (int, Boolean, etc.)
Copyright (C) 2015 Mr.Simple <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.