EventBus can transfer data between Activity Fragment Service threads instead of Intent Broadcast Handler
Related knowledge
Four threadmodes
POSTING: the thread in which the post is published and the event processing function is located
MAIN: event handler runs in UI thread
BACKGROUND: if the event is published in the UI thread, the event handler will run in the new thread. If the event is published in the sub thread, it will run in the published sub thread. The event handler prohibits updating the UI
ASYNC: no matter which thread is published, it will run in the new thread
Registration and cancellation
EventBus.getDefault().register(this) registration
EventBus.getDefault().unregister(this) logout
EventBus.getDefault().isRegister(this) determines whether it is registered true registered false not registered
It's recommended to register to unregister OnCreate before registering with OnDestroy
Sticky event
sticky can send events first and then register
@Subscribe: event subscriber, receiving messages from event publisher
@Publisher: event publisher, directly calling EventBus.getDafault().post(obj)
Ordinary events
public class MainActivity extends AppCompatActivity { private AppCompatButton btn_Event; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_Event = findViewById(R.id.btn_event); btn_Event.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this,SecondActivity.class)); } }); //register if (!EventBus.getDefault().isRegistered(this)) { EventBus.getDefault().register(this); } } //Event handler @Subscribe(threadMode = ThreadMode.MAIN) public void onEventFuc(Message message){ btn_Event.setText(message.getMessage()); } @Override protected void onDestroy() { super.onDestroy(); //Cancellation EventBus.getDefault().unregister(this); } }
public class SecondActivity extends AppCompatActivity { private AppCompatButton btn_message; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); btn_message = findViewById(R.id.btn_message); btn_message.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Release events EventBus.getDefault().post(new Message("I released the event")); finish(); } }); } }
Sticky event
public class MainActivity extends AppCompatActivity { private AppCompatButton btn_Event, btn_register; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_Event = findViewById(R.id.btn_event); btn_register = findViewById(R.id.btn_register); btn_Event.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, SecondActivity.class)); } }); btn_register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //register if (!EventBus.getDefault().isRegistered(this)) { EventBus.getDefault().register(MainActivity.this); } } }); } //Event handler @Subscribe(threadMode = ThreadMode.MAIN, sticky = true) public void onEventFuc(Message message) { btn_Event.setText(message.getMessage()); } @Override protected void onDestroy() { super.onDestroy(); //Cancellation EventBus.getDefault().unregister(this); } }
Add more sticky = true to the handler
public class SecondActivity extends AppCompatActivity { private AppCompatButton btn_message; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); btn_message = findViewById(R.id.btn_message); btn_message.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Release events EventBus.getDefault().postSticky(new Message("I'm releasing sticky events")); finish(); } }); } }