Harry T.
Harry T.

Reputation: 3527

EventBus in custom view

I'm customing a View and I want to listening event from there. I created a show() and hide() method then put register & unregister inside these methods. But i tried to put

public class CalculatorView extends RelativeLayout {
.......

public void show() {
    Log.i("hieu", "eventBus register");
    EventBus.getDefault().register(this);
}

public void hide() {
    EventBus.getDefault().unregister(this);
    Log.i("hieu", "eventBus unreg");
}

@Subscribe(threadMode = ThreadMode.MAIN) 
public void onEvent(EventBusMessage eventBusMessage) 
{ Log.i("hieu", ""); }

but it didn't jumped in onEvent. I'm using EventBus 3.0.0. How to archive this? Thanks.

Upvotes: 3

Views: 1237

Answers (1)

wing zjq
wing zjq

Reputation: 81

public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
    super(Application.class);
}

public void testRun() {
    CalculatorView view = new CalculatorView(getContext());
    view.show();
    EventBus.getDefault().post(new EventBusMessage());
    view.hide();
}

public class CalculatorView extends RelativeLayout {

    public CalculatorView(Context context) {
        super(context);
    }

    public void show() {
        Log.i("hieu", "eventBus register");
        EventBus.getDefault().register(this);
    }

    public void hide() {
        EventBus.getDefault().unregister(this);
        Log.i("hieu", "eventBus unreg");
    }

    public void onEvent(EventBusMessage eventBusMessage) {
        Log.i("hieu", "");
    }
}

class EventBusMessage {

}

}

can not this work?

enter image description here

it work on my test code.

Upvotes: 1

Related Questions