Android EventBus3.0深入理解 三 使用多个EventBus

EventBus.getDefault()是通过单例模式来获取一个默认实例,EventBus允许我们创建EventBus实例,我个人觉得自己创建的EventBus基本要保持全局,所以我自己单独弄一个类使用单例模式来创建和获取EventBus

public class EventBusBase {
    private static EventBus eventBus;

    public static EventBus getInstance(){
        if(eventBus == null){
            synchronized (EventBus.class){
                if(eventBus == null)
                    eventBus = new EventBus();
            }
        }
        return eventBus;
    }
}




多个EventBus可以同时使用,没问题,试过,多个EventBus可以完成传送不同的消息,完成不同的自定义化,而且同一个activity、fragment使用多个EventBus完全没问题,例子如下:

public class MainActivity extends AppCompatActivity {

    TextView tv_msg;
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        Button btn_toTwo = (Button)findViewById(R.id.btn_toTwo);
        btn_toTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,TwoActivity.class);
                //startActivity(intent);
                TestBean bean = new TestBean();
                bean.msg = "MainActivity TestBean";
                EventBus.getDefault().post(bean);
            }
        });

        tv_msg = (TextView)findViewById(R.id.tv_msg);



        Button btn_sendMsg = (Button)findViewById(R.id.btn_sendMsg);
        btn_sendMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TwoBean bean = new TwoBean();
                bean.msg = "MainActivity TwoBean";
                EventBusBase.getInstance().post(bean);
            }
        });



    }
    @Subscribe(priority = 3,threadMode = ThreadMode.MAIN)
    public void onMessageEvent(TestBean event) {
        Log.v("zzw","MainActivity");

    }


    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }


}
public class ThreeActivity extends AppCompatActivity {


    TextView tv_msg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_three);
        EventBusBase.getInstance().register(this);
        EventBus.getDefault().register(this);
        Button btn_sendMsg = (Button)findViewById(R.id.btn_sendMsg);
        btn_sendMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TestBean bean = new TestBean();
                bean.id = 3;
                bean.msg = "来自ThreeActivity的消息";
                EventBus.getDefault().postSticky(bean);
                Toast.makeText(ThreeActivity.this,bean.msg,Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(ThreeActivity.this,TwoActivity.class);
                startActivity(intent);

            }
        });

        tv_msg = (TextView)findViewById(R.id.tv_msg);
    }
    @Override
    public void onDestroy() {
        EventBusBase.getInstance().unregister(this);
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(TestBean event) {
        Log.v("zzw",event.msg);
        tv_msg.setText(event.msg);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(TwoBean event) {
        Log.v("zzw",event.msg);
    }
}



这个框架坑很多,但是实在是方便,大家还是多花功夫去研究吧



你可能感兴趣的:(Android框架)