Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GreenRobot: EventBus's isRegistered() method not working as expected #355

Closed
lalit3686 opened this issue Sep 21, 2016 · 4 comments
Closed

Comments

@lalit3686
Copy link

I am using EventBus for receiving the events. I want to check if my Activity is already registered or not as I need to register only once through the whole lifetime of the application, but the issue is that wheneven I come to that Activity which is registered EventBus registers it again and due to that multiple events are getting fired.

Below is my code sample!

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

Also, find the screenshot of logs, in which you can see that initially it gives me proper response but once I move to that Activity again it registers the subscriber again!

NOTE: Please don't suggest me to unregister as I want it to be registered always!

enter image description here

@william-ferguson-au
Copy link
Contributor

Are you sure that this is the same each time?
Try changing your log to
Log.e(TAG, "already registered event bus for " + this);

If your MainActivity is being destroyed and recreated (eg due to rotation) then it will be a different instance each time.

Also, when/where are you registering?
When/where are you unregistering?

@lalit3686
Copy link
Author

Instance is different everytime as I am destroying the Activity and creating it everytime and registering it but why I am getting mutliple logs though instance is different?

If Activity 1 is destroyed and and I open it again say Activity 2 then why it is sending me the event registered with Activity 1?

@william-ferguson-au
Copy link
Contributor

Because if you never unregistered Activity1 then EVentBus still has a reference to it, so will still send it messages.

You should ALWAYS unregister your subscriber at the equivalent exit point.

@lalit3686
Copy link
Author

lalit3686 commented Sep 21, 2016

ok, got it thanks for the brief explanation!

It would be good that you answer on StackOverflow as well to keep for future users - http://stackoverflow.com/questions/39589046/greenrobot-eventbuss-isregistered-method-not-working-as-expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants