Eventbus onMessageEvent not getting called

2019-07-10 02:33发布

问题:

I have implemented EventBus in my project but I am not getting all of my events

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new MessageEvent());
                EventBus.getDefault().post(new MessageEvent2());
            }
        });
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event)
    {
        Toast.makeText(this, "MainActivity called", Toast.LENGTH_SHORT).show();
    };

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
}

Here i created 2 event inside onClick(); And this is my AnotherActivity where i have another @Subscribe

public class AnotherActivity extends AppCompatActivity {

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

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent2 event2)
    {
        Toast.makeText(this, "AnotherActivity called", Toast.LENGTH_SHORT).show();//Not getting called
    };

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
}

I dont know why my second toast is not getting called, i have done every thing correctly.

What i suspect is the AnotherActivity is not created yet so my event is not called is that is so what is use of EventBus then?

回答1:

What i suspect is the AnotherActivity is not created yet so my event is not called is that is so

Yes, if the event happened in past and the component(activity) is not active/created then the event will not be received.

what is use of EventBus then?

You can use Sticky Event to listen to past events in newly created activity

so use postSticky

EventBus.getDefault().postSticky(new MessageEvent2());

and add sticky = true in subscribe annotation

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent2 event2){
    Toast.makeText(this, "AnotherActivity called", Toast.LENGTH_SHORT).show();//Not getting called
};

or you can receive them manually

MessageEvent2 msg2 = EventBus.getDefault().getStickyEvent(MessageEvent2.class);
// you can also remove it using
// EventBus.getDefault().removeStickyEvent(msg2);

or remove it from history as well

MessageEvent2 msg2 = EventBus.getDefault().removeStickyEvent(MessageEvent2.class);
if(msg2!=null){//do something}