Android greenrobot: the isregistered() method of eventbus does not work as expected

I am using eventbus to receive events. I want to check whether my activity has been registered because I only need to register once in the whole life cycle of the application, but the problem is that even if I come to the registered activity, eventbus will register it again and be fired due to the multiple event

The following is my code example!

    public void registerEventBus(){
        if(EventBus.getDefault().isRegistered(this)){
            Log.e(TAG, "already registered event bus for "+TAG);
        }
        else{
            EventBus.getDefault().register(this);
            Log.e(TAG, "registering event bus for "+TAG);
        }
    }

In addition, find the screenshot of the log, where you can see that it initially gave me the appropriate response, but once I go to the activity again, it will register the subscriber again!

Note: please do not advise me to sign out because I want it to always be registered!

Also answer on GitHub- https://github.com/greenrobot/EventBus/issues/355

resolvent:

If your activity is destroyed and recreated (for example, during rotation), a new instance of your activity will be registered in eventbus

If you do not unregister the old instance during the corresponding exit point (onpause / onstop / ondestroy), an event is sent to two activities

Confirm to change the log to

Log.e(TAG, "already registered event bus for " + this);

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>