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

后端 未结 4 1282
迷失自我
迷失自我 2021-02-07 00:00

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

  1. onCre
相关标签:
4条回答
  • 2021-02-07 00:03

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

    0 讨论(0)
  • 2021-02-07 00:16

    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

    0 讨论(0)
  • 2021-02-07 00:27

    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.

    0 讨论(0)
  • 2021-02-07 00:28

    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");
        }
    }
    
    0 讨论(0)
提交回复
热议问题