Android EventBus 3.0 deep understanding three uses of multiple EventBus

EventBus.getDefault() is to obtain a default instance through the singleton mode. EventBus allows us to create an EventBus instance. I personally feel that the EventBus created by myself basically needs to remain global, so I make a class by myself to use singleton mode to create and obtain 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;
    }
}




Multiple eventbuss can be used at the same time, no problem. Once tried, multiple eventbuss can complete the transmission of different messages, complete different customization, and the use of multiple eventbuss for the same activity and fragment is completely OK. The example is as follows:

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 = "Come from ThreeActivity Message for";
                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);
    }
}



There are many holes in this framework, but it's really convenient. Let's spend more time to study it



Tags: Fragment

Posted on Sun, 03 May 2020 16:49:29 -0400 by amberb617