3

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.

Harry T.
  • 3,478
  • 2
  • 21
  • 41

1 Answers1

1
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.

wing zjq
  • 81
  • 5
  • Yes. It didn't work. I don't know why but it didn't jump in onEvent. – Harry T. Sep 26 '17 at 02:56
  • Ya it works again. I realized that Logcat doesn't log empty string, it made me think that it didn't work. Thanks for helping me. And your onEvent method need @Subscribe, at least in version 3.0.0. – Harry T. Sep 26 '17 at 03:53