Which Activity lifecycle methods are best to register/unregister to event bus?

£可爱£侵袭症+ 提交于 2019-12-03 14:48:56

问题


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.


回答1:


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




回答2:


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.




回答3:


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




回答4:


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");
    }
}


来源:https://stackoverflow.com/questions/29236704/which-activity-lifecycle-methods-are-best-to-register-unregister-to-event-bus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!