Primary use of EventBus -- message passing between activities

For the data communication between activities with large amount of data, avoid using Intent + Parcelable
In order to avoid transaction to largeexception, alternative schemes such as EventBus can be considered.

EventBus is a publish / subscribe event bus mechanism under Android. Instead of Intent, Handler, Broadcast, etc., it can deliver messages between fragments and activities.

Advantages: low overhead, elegant code. Decouple sender and receiver.

The first step is to import dependency first

 implementation 'org.greenrobot:eventbus:3.1.1'

Custom event class

public class MessageEvent {
    private String message;

    public MessageEvent(String message) {
        this.message = message;
    }

    public String getMessage(){
        return message;
    }
    public void getMessage(String message){
        this.message = message;
    }
}

Register event code

        //Register EventBus
        EventBus.getDefault().register(this);

Cancellation of registration

        //Unregister EventBus
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }

Sending events

EventBus.getDefault().post(new MessageEvent("This is the event sent through the second page"));

Handling events

The name of the method handling the message can be taken at will. But you need to add an annotation @ Subscribe and specify the thread model.

  @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent) {
         main_tv.setText(messageEvent.getMessage());
    }

Here is the complete code

Main page

ublic class MainActivity extends AppCompatActivity {

    private TextView main_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Register EventBus
        EventBus.getDefault().register(this);
        main_tv = findViewById(R.id.activity_main_tv);


    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent) {
         main_tv.setText(messageEvent.getMessage());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Unregister EventBus
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }

    //Click the button to transfer to the second Activity
    public void jumpToSecond(View view) {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        startActivity(intent);
    }
}

Second page

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);


    }
     // Button click event
    public void sendMessage(View view) {
        //Send events to first page
        EventBus.getDefault().post(new MessageEvent("This is the event sent through the second page"));
        //Close current Activity
        finish();

    }
}

Message processing class

public class MessageEvent {
    private String message;

    public MessageEvent(String message) {
        this.message = message;
    }

    public String getMessage(){
        return message;
    }
    public void getMessage(String message){
        this.message = message;
    }
}

Tags: Mobile Android

Posted on Mon, 02 Dec 2019 02:10:23 -0800 by indigo2k