@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Butterknife.bind(this);
textView.setText(name);
//注册
EventBus.getDefault().register(this);
}
@OnClick(value = {R.id.start_tv, R.id.start_two_tv})
void start_two(View view) {
switch (view.getId()) {
case R.id.start_tv:
startActivity(new Intent(this, Main2Activity.class));
break;
case R.id.start_two_tv:
Toast.makeText(this, "我被点击了", Toast.LENGTH_LONG).show();
break;
}
}
//接受,注意这个方法使用public修饰,如果希望接受粘性事件的话,我们必须sticky = true
@Subscribe(threadMode = ThreadMode.ASYNC, priority = 100, sticky = true)
public void getLogiBean(LoginBean loginBean) {
Log.e("tag", "接受到了" + loginBean.toString());
}
@Override
protected void onDestroy() {
super.onDestroy();
//取消注册
EventBus.getDefault().unregister(this);
}
activity2
package com.ali.reflectdemotwo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import org.greenrobot.eventbus.EventBus;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
@Deprecated
public void send(View view) {
//发送
EventBus.getDefault().post(new LoginBean("wang", 200));
//发送粘性事件
EventBus.getDefault().postSticky(new LoginBean("wang", 200));
}
}