I have a use case where I need to know the currently active realtime listeners in the client. This is so that I can remove the listeners from anywhere in the code. I attached the listener in activity A and would need to remove it in activity B when an event occurs. Is there a way I can achieve this?
1 Answers
I have a use case where I need to know the currently active realtime listeners in the client. This is so that I can remove the listeners from anywhere in the code.
In my opinion, there is no need to know how many listeners are currently active, as long as you remove them according to the life-cycle of your activity.
I attached the listener in activity A and would need to remove it in activity B when an event occurs.
You should always remove the listener once you leave activity B. If you need to take some action when an event occurs in activity B, then you should add another listener, which is different than the listener in activity A.
Please also note that Firestore can clean up the all listeners automatically when the activity is stopped if you are passing as the fist argument your activity:
ListenerRegistration registration = docRef.addSnapshotListener(ActivityA.this, listener);

- 130,605
- 17
- 163
- 193
-
In my case, I need to permanently detach the listener created in activity A when an event occurs in Activity B. – Urchboy Nov 20 '19 at 13:28
-
What is the event that occurs in `activity B`? – Alex Mamo Nov 20 '19 at 13:35
-
1Okay Alex, I found a way out. I would have to check if the event has occured from the activity A. The event is simply to check how many persons the user has referred... I did not realize I could also check in activity A. Thanks – Urchboy Nov 20 '19 at 13:44