I am adding ValueEventListener in my activity of android.I am worrying if my app crashes or anyhow, onDestroy isn't called then my listeners may not be removed manually.I also used system.exit in somewhere,I didn't used them in service though .now I am worrying if they aren't removed and my application is closed,will they be still connected and increase my firebase billing?
-
Have you tested it by yourself ? – Ticherhaz FreePalestine Jul 17 '21 at 09:34
-
no,just worrying because i am not so expert yet.And it seems If I test,I may not get accurate result – Apurba A Jul 17 '21 at 10:33
3 Answers
This is a 'yes but no' answer, according to the documentation, all listeners are registered on the server which invokes a trigger to send the updated information. Since the listener is not unregistered, it is therefore not 'removed'. Instead, the server will detect that the client has timed out after a few attempts and will stop sending requests.
Effectively they are the same thing, only delayed. relaunching the app will not reconnect to the listeners if the session ID does not match.

- 4,225
- 1
- 12
- 20
-
so android will stop them and my billing will not be increasing continuously (horror bill muhahaha ) to 10000usd or infinity,it may cause additional 10-50 USD,right? – Apurba A Jul 17 '21 at 21:08
-
also ,if app is relaunched and listeners are added again,then will I be charged twice if old ones are not removed? – Apurba A Jul 17 '21 at 21:13
-
Android OS will handle this as well as the Firebase Server's, this edge case has been developed to prevent infinite billing loops. – DIGI Byte Jul 18 '21 at 02:03
-
Last question:basically ,i have only single activity,so they are automatically removed after application closed,(unfortunately i can't remove the listeners according to my app) and I will not face too much problem with payment,right – Apurba A Jul 18 '21 at 06:24
-
It would be ideal to manage it where possible, but in general, you should be fine. – DIGI Byte Jul 18 '21 at 08:11
When you are using a ValueEventListener, which means that:
Classes implementing this interface can be used to receive events about data changes at a location.
If you are using addValueEventListener(ValueEventListener) method, it means that you are listening for changes in real-time. This also means, that if you don't remove the listener according to the life-cycle of the activity, the listener will remain active, until Android OS will eventually close it. This means that as long as the listener is active, the "onDataChange()" will always fire providing a snapshot of the data at the location the reference is pointing to.
It's true that there is no guarantee that "onDestroy()" will be called. So most likely you should consider using addListenerForSingleValueEvent():
Add a listener for a single change in the data at this location.
Case in which there is no lister that has to be removed.

- 130,605
- 17
- 163
- 193
-
actually i need valueeventlistener instead of singlevalueevent Do you know any solution? can I use anything which will be null after activity doesn't exists? so that it will then call catch block ,and I will add removelistener in that catch block how good/bad is the idea? – Apurba A Jul 17 '21 at 14:50
-
If you need to listen for real-time changes, then you should remove the listener in onStop or onPause. Or you can use the [Firebase-UI library](https://github.com/firebase/FirebaseUI-Android), and start/stop listening for changes in the onStart/onStop. Give it a try and tell me if it works. – Alex Mamo Jul 17 '21 at 14:55
-
onStop will be called even when phone is being off for no power or anything else,right? I am not confident enough,so asking – Apurba A Jul 17 '21 at 16:48
-
Check these [answers](https://stackoverflow.com/questions/38349219/activity-crash-lifecycle-method-android), it will clear your thoughts, right? – Alex Mamo Jul 17 '21 at 16:54
-
basically these crash can happen and end the app even before calling onStop,right? then we can't do anything,we need to only rely android system so that they can remove these listeners – Apurba A Jul 17 '21 at 20:10
-
-
just last time interrupting, so android will stop them and my billing will not be increasing continuously (horror bill muhahaha ) to 10000usd or infinity,it may cause additional 10-50 USD,right? – Apurba A Jul 17 '21 at 21:07
-
Android will eventually stop the process in favor of others but the best practice is tu always remove the listener,. In that case you won't be charged with unnecessary resources. – Alex Mamo Jul 17 '21 at 21:32
-
-
sorry,one more time interrupting you: basically ,i have only single activity,so they are automatically removed after application closed, and I will not face problem too much problem with payment,right? (unfortunately i can't remove the listeners according to my app) – Apurba A Jul 18 '21 at 06:25
-
If you close an application without removing the listener, it will remain active causing a waste of bandwidth. So if you stop listening for changes in onStop and you close the app, there will indeed no problems. – Alex Mamo Jul 18 '21 at 06:29
-
Yup Accepted,bro "causing a waste of bandwidth." will it charge me too? (I am noob in development) – Apurba A Jul 18 '21 at 07:52
According to your problem. You are passing a callback to get the data changes. So simply follow the Activity lifecycle, onResume()
you can set the callback listener & onStop()
remove the callback listener. If your app crash, there is no guarantee that your activity going to maintain the lifecycle, so you must handle the crash at your own. Simply use try catch

- 352
- 3
- 11