Which Activity lifecycle methods are best to regis

2019-03-17 20:37发布

What is the best place to register and unregister to an event bus (like otto, EventBus, or tinybus) in an Activity and why?

  1. onCreate()-onDestroy()
  2. onStart()-onStop()
  3. onResume()-onPause()

Otto's example uses onResume()-onPause(), EventBus's mentions onStart()-onStop(), and we needed to use onCreate()-onDestroy() in our app to update the activity's UI even when it was in the background. So I guess it can be any of the three depending on the nature of the events and their handling, but I was wondering if there is anything more to it that should be considered.

4条回答
贪生不怕死
2楼-- · 2019-03-17 20:41

I removed my comment in the above answer that it would be best to register / unregister in onresume/onpause. I got a strange usecase where some if my events werent reaching the annotated subscriber. Seems the best way is to use the onstart / onstop. Here is a good SO post explaining why:

https://stackoverflow.com/a/19737191/2361947

查看更多
在下西门庆
3楼-- · 2019-03-17 20:47

First of all, its not a objective question rather its a subjective one and will draw lots of arguments based on arguments.

From my experience, We used Otto in one of our project. We followed onResume()-onPause() which served us very good. It also makes sense cause we should register as late as possible & deregister as fast as possible while using an event bus.

查看更多
Evening l夕情丶
4楼-- · 2019-03-17 20:54

@levavare, I think the correct time to register/unregister depends on your events and what you intend to do with them. And can be different for different events within the same application.

For example, I'm using EventBus in an Android app that monitors a realtime data logging device (Arduino, in this case) via Bluetooth. I have two quite different types of events.

The first event is posted by my Bluetooth code to notify one of my fragments that a new set of instrument readings has been received from the device. That fragment then writes them to a database table. It's important that event always be heard and acted on. The fragment registers/unregisters in its OnCreate/OnDestroy methods. I also subscribe for that event with elevated priority.

The other event is posted by the database layer when the new record is added to the database. I have a series of fragments that show different subsets of the readings (temperatures, pressures, alarm conditions). When one of those fragments is being viewed it should update as soon as the new reading is in the database. But when the fragment is out of sight, there's no reason for it to act on a reading. I have those fragments register/unregister in OnStart/OnStop. I was going to do that work in OnResume/OnPause and, frankly, I think it would work as well there for my app. But @Jordy's answer and link convinced me to go with OnStart/OnStop instead.

查看更多
Luminary・发光体
5楼-- · 2019-03-17 21:00

Form EventBus Documentation that I found and it's works fine for me:

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

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

And if you need to send the EventBus reference to the child then:

private EventBus eventBus = EventBus.getDefault();
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        .......
}

@Override
public void onStart() {
    super.onStart();
    if(!eventBus.isRegistered(this)){
        eventBus.register(this);
    }else{
        Log.e(TAG, "EventBus is registered");
    }
}

@Override
public void onStop() {
    super.onStop();
    if(eventBus.isRegistered(this)){
       eventBus.unregister(this);
    }else{
       Log.e(TAG, "EventBus is not registered");
    }
}
查看更多
登录 后发表回答