简体   繁体   English

具有活动/片段生命周期的EventBus行为

[英]EventBus behavior with Activity/Fragment lifecycle

I have registered my activity/fragment like this: 我已经这样注册我的活动/片段:

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

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

I have a process which is running and posting events with its status to my subcriber method. 我有一个正在运行的进程,并将其状态发布到我的订阅者方法中的事件。

For instance, if the user rotates the device, onStop will be called and my activity/fragment will be unregistered and it will not listen to my process events until it register again. 例如,如果用户旋转设备,则将调用onStop,并且我的活动/片段将不被注册,并且直到再次注册它都不会监听我的过程事件。

If some events are posted in this short time, what will happen? 如果在短时间内发布一些事件,将会发生什么?

(considering that my event bus will not throw any exception because of not having any subcriber registered.) (考虑到我的事件总线不会因未注册任何订阅者而引发任何异常。)

I really want to handle situations like that when for some reason the activity/fragment is not registered and some events are being posted. 我真的想处理这样的情况,当由于某种原因活动/片段未注册并且某些事件正在发布时。

I was thinking about implementing a queue of events that are not received by subscribers so that the UI could do somthing about it when it gets registered again. 我正在考虑实现订阅者未收到的事件队列,以便UI可以在再次注册时对其进行处理。

You need to register in the onCreate() and unregister in onDestroy() , something like this: 您需要在onCreate()注册,并在onDestroy()注销,如下所示:

@Override 
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ...
  // Check if Event Bus is not already register here
  if (!EventBus.getDefault().isRegistered(this)) {
        EventBus.getDefault().register(this);
     }
  ...
}

@Override 
protected void onDestroy() {
  // Check if Event Bus is register here, then unregister it.
  if (EventBus.getDefault().isRegistered(this)) {
      EventBus.getDefault().unregister(this);
     }
  super.onDestroy();
}

You have two types of events: - the ones that signal that something happened and are not really relevant later. 您有两种类型的事件:-表示已发生某事并且以后没有真正意义的事件。 For example: car speed became 50 km/h - the other ones that are more like states. 例如:汽车速度变成了50 km / h-其他速度更像是州。 For example the car engine is turned off. 例如,汽车发动机已关闭。

If event of the first type is posted, it is usually not a big deal to miss, because there will be another one that will replace the old value (ie the speed became 30 km/h). 如果发布了第一种类型的事件,通常不会错过很多,因为会有另一种事件代替旧值(即速度变为30 km / h)。 For the second type of events, you probably want to know the last value (or state) because it may not change again very soon. 对于第二类事件,您可能想知道最后一个值(或状态),因为它可能很快不会再次更改。 (with the example above, you want to know if you missed the event that the engine was turned off or on, in order to do/show something specific) (对于上面的示例,您想知道是否错过了引擎关闭或打开的事件,以便执行/显示特​​定的操作)

This second type of events is usually handled as sticky, for which you use postSticky() and registerSticky() methods. 第二种类型的事件通常被视为粘性事件,对此您可以使用postSticky()registerSticky()方法。 So if your events are posted as sticky, after you call registerSticky() you will get notified for all the sticky events with their last value. 因此,如果您的事件发布为粘性事件,则在调用registerSticky()您将收到所有粘性事件的最新值通知。

If the last value per event is not enough, then probably you will need to cache the missed events manually. 如果每个事件的最后一个值还不够,那么可能需要手动缓存丢失的事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM