EventBus 3.0 的三大要素
EventBus is an open-source library for Android and Java using the publisher/subscriber pattern for loose coupling. EventBus enables central communication to decoupled classes with just a few lines of code – simplifying the code, removing dependencies, and speeding up app development.
EventBus 3.0 三步走
1. Define Events
public class MessageEvent {
public final String message;
public MessageEvent(String message) {
this.message = message;
}
}
2.Prepare subscribers
Firstly, we need to register/unregister in the lifecycle of the activity or fragment. Such as:
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
protected void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
onResume() / onPause();
Subscribers implement event handling methods (also called “subscriber methods”) that will be called when an event is posted. These are defined with the @Subscribe annotation.
Note that with EventBus 3 the method name can be chosen freely
// This method will be called when a MessageEvent is posted (in the UI thread for Toast)
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
Toast.makeText(getActivity(), event.message, Toast.LENGTH_SHORT).show();
}
// This method will be called when a SomeOtherEvent is posted
@Subscribe
public void handleSomethingElse(SomeOtherEvent event) {
doSomethingWith(event);
}
3.Post events
EventBus.getDefault().post(new MessageEvent("Hello everyone!"));
When we have 2 activities, activity A and activity B, we hope that activity B post() the value to the activity A after clicking button. At this time, we can't put the post() in the onClick().
Activity B:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
......
Button mButton2 = findViewById(R.id.button2);
mButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//EventBus.getDefault().post(new MessageEvent("EventBus送我过来的!"));
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
We could put the post() in the onStop(), because before posting we need to register() in the target activity at first.
@Override
protected void onStop() {
super.onStop();
EventBus.getDefault().post(new MessageEvent("EventBus送我过来的!"));
}
Activity A:
public class MainActivity extends AppCompatActivity {
private String TAG = "MainActivity";
public TextView mTextView;
public Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("MainActivity");
mTextView = findViewById(R.id.tv_mainActivity);
mButton = findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
mTextView.setText(event.message);
}
}
In Activity B, when we click Button2 we will open the Activity A, then the Activity A will run onCreate(), onStart(). At last the Activity B will run onStop().