Android EventBus usage

I encountered a small problem today. I wanted to pass the value to the activity in the adapter. I was wondering if there is a set of [open source projects] to solve this problem. I searched and found that there is really

I mainly describe the aspects I use, but I will not be very comprehensive.

Detailed explanation of EventBus usage – short bookOverview EventBus is an Android event publishing/subscribing framework that simplifies Android event delivery by decoupling publishers and subscribers, where events can be understood as messages. Event delivery can be used for both Android four…/p/a040955194fc

First of all, there must be an entity class, which is used to temporarily store data. For example, I am afraid that this will be too messy, so I will write it directly in the activity file.

class  Event {

    private String url,name;
    public String getName() {
        return name;
    public void setName(String name) { = name;
    public String getUrl() {
        return url;
    public void setUrl(String url) {
        this.url = url;

Then it’s simple, register it in the activity’s onCreate

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

At the same time, remember to delete the registration when you log out of the page.

    protected void onDestroy() {

After we are ready, we start to transfer data. This transfer place can be adapter or [fragment] , it should be fine, how to say it is not transfer, just put it in entity class

Event event = new Event();

Then we get the data in the entity class through [eventBus . This is to be written in the activity, and there is no need to call it again.]

    public void onEvent(Event event) {/* Do something */
        String name = event.getName();
        String url = event.getUrl();
        Log.e( "click" ,name+ ":" +url);

This actually ends the running out of the debugging information as follows

2022 -02 -12  13 : 45 : 02.046  15593 -15593 /cn.titansys.xview_pptv E/click: CCTV2
 2022 -02 -12  13 : 45 : 02.080  15593 -15593 /cn.titansys.xview_pptv E /click: CCTV2:udp :/ / 239.45 .0 .1 : 5140

I wrote it directly into the project. There is a lot of code, so I won’t show it. If you don’t understand, you can read the link above.

Leave a Comment

Your email address will not be published. Required fields are marked *