If you not sure, please click here : EventBus
- EventBus is a publish/subscribe event bus for Android and Java.
- simplifies the communication between components
- makes your code simpler
- is fast
- is tiny (~50k jar)
- has advanced features like delivery threads, subscriber priorities, etc.
android {
// ......
defaultConfig {
// ......
javaCompileOptions {
annotationProcessorOptions {
arguments = [packageName: 'com.cmonbaby.eventbus.apt', className: 'EventBusIndex']
}
}
}
buildTypes {
release {
// ......
}
}
}
2. Prepare subscribers: Declare and annotate your subscribing method, optionally specify a thread mode:
@Subscribe(threadMode = ThreadMode.MAIN)
public void getUser(UserInfo info) { }
EventBus.getDefault().post(new UserInfo("simon", 35));
// or
EventBus.getDefault().postSticky(new UserInfo("simon", 35));
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create indexes for apt
EventBus.getDefault().addIndex(new EventBusIndex());
// register listener
EventBus.getDefault().register(this);
}
// post event
public void post() {
EventBus.getDefault().post(new UserInfo("simon", 35));
}
// post sticky event
public void sticky() {
EventBus.getDefault().postSticky(new UserInfo("simon", 35));
}
// The higher number the quantity, the higher the priority
@Subscribe(threadMode = ThreadMode.MAIN, priority = 1)
public void getUser(UserInfo info) { // subscribe method
// Do something
}
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void sticky(UserInfo user) { // subscribe sticky method
Log.e("sticky", user.toString());
}
@Override
protected void onDestroy() {
super.onDestroy();
// destroy eventbus
if (EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().unregister(this);
}
EventBus.clearCaches();
}
}
Via Gradle:
implementation 'com.cmonbaby.eventbus.core:eventbus_core:1.2.0'
implementation 'com.cmonbaby.eventbus.annotation:eventbus_annotation:1.2.0'
annotationProcessor 'com.cmonbaby.eventbus.compiler:eventbus_compiler:1.2.0'
Via Maven:
<dependency>
<groupId>com.cmonbaby.eventbus.annotation</groupId>
<artifactId>eventbus_annotation</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.cmonbaby.eventbus.compiler</groupId>
<artifactId>eventbus_compiler</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.cmonbaby.eventbus.core</groupId>
<artifactId>eventbus_core</artifactId>
<version>1.2.0</version>
</dependency>
Copyright (C) 2013-2020 Markus Junginger, Simon (https://www.cmonbaby.com)
EventBus binaries and source code can be used according to the Apache License, Version 2.0.