誰接收消息誰就註冊解註冊ide
// eventbus
public class EventBusActivity extends Activity { private TextView tv_title; private Button bt_eventbus_send; private Button bt_eventbus_sticky; private TextView tv_eventbus_result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_event_bus); initView(); initData(); initListener(); } private void initListener() { // 跳轉到發送頁面
bt_eventbus_send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(EventBusActivity.this, EventBusSendActivity.class); startActivity(intent); } }); // 發送粘性事件到發送頁面
bt_eventbus_sticky.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 2 發送粘性事件
EventBus.getDefault().postSticky(new StickyEvent("我是粘性事件")); // 跳轉到發送數據頁面
Intent intent = new Intent(EventBusActivity.this, EventBusSendActivity.class); startActivity(intent); } }); } private void initData() { tv_title.setText("EventBus"); // 1註冊廣播
EventBus.getDefault().register(EventBusActivity.this); } private void initView() { tv_title = (TextView)findViewById(R.id.tv_title); bt_eventbus_send = (Button)findViewById(R.id.bt_eventbus_send); bt_eventbus_sticky = (Button)findViewById(R.id.bt_eventbus_sticky); tv_eventbus_result = (TextView)findViewById(R.id.tv_eventbus_result); } // 5接收消息
@Subscribe(threadMode = ThreadMode.MAIN) public void MesssageEventBus(MessageEvent event){ // 顯示接收的消息
tv_eventbus_result.setText(event.name); } @Override protected void onDestroy() { super.onDestroy(); // 2 解註冊
EventBus.getDefault().unregister(EventBusActivity.this); } }
// eventbus的發送數據頁面
public class EventBusSendActivity extends Activity { private TextView tv_title; private Button bt_eventbus_send_main; private Button bt_eventbus_send_sticky; private TextView tv_eventbus_send_result; boolean isFirstFlag = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_event_bus_send); initView(); initData(); initListener(); } private void initListener() { // 主線程發送數據按鈕點擊事件處理
bt_eventbus_send_main.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 4 發送消息
EventBus.getDefault().post(new MessageEvent("主線程發送過來的數據")); // 結束當前頁面
finish(); } }); // 接收粘性事件數據按鈕的點擊事件處理
bt_eventbus_send_sticky.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(isFirstFlag) { isFirstFlag = false; // 4 註冊
EventBus.getDefault().register(EventBusSendActivity.this); } } }); } // 3 接收粘性事件
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true) public void StickyEventBus(StickyEvent event){ // 顯示接收的數據
tv_eventbus_send_result.setText(event.msg); } private void initData() { tv_title.setText("EventBus發送數據頁面"); } private void initView() { tv_title = (TextView)findViewById(R.id.tv_title); bt_eventbus_send_main = (Button)findViewById(R.id.bt_eventbus_send_main); bt_eventbus_send_sticky = (Button)findViewById(R.id.bt_eventbus_send_sticky); tv_eventbus_send_result = (TextView)findViewById(R.id.tv_eventbus_send_result); } @Override protected void onDestroy() { super.onDestroy(); // 5 解註冊
EventBus.getDefault().removeAllStickyEvents(); EventBus.getDefault().unregister(EventBusSendActivity.this); } }