Should I actually remove the ValueEventListener?
When talking about listeners, yes, you need to remove them accordingly to the life-cycle of your activity and for this you need to use the following line of code:
databaseReference.removeEventListener(valueEventListener);
Remember if you don't do this, you'll end up wasting your battery and bandwidth. So:
- If you have added the listener in
onStart
you have to remove it inonStop
. - If you have added the listener in
onResume
you have to remove it inonPause
. - If you have added the listener in
onCreate
you have to remove it inonDestroy
.
But remember onDestroy
is not
always called, so the last option in not always a good choice.
There is another approach in which there is no need to remove the listener and that is when using addListenerForSingleValueEvent:
Add a listener for a single change in the data at this location.
Is it necessary to remove each added listener from Firebase Realtime Database?
When you are using "addValueEventListener()", it means that you are attaching a persistent listener to a specific location in the database to get data in real-time. It doesn't really matter if you are adding the listener in a Coroutine context or not, you always need to remove the listener accordingly to the lifecycle of your activity.
If you don't need real-time updates, you can use addListenerForSingleValueEvent(), as it:
Adds a listener for a single change in the data at this location.
So there is no need to remove any listener at all.
You can also use the relatively new added get() method, as explained in one of my articles:
- How to read data from Firebase Realtime Database using get()?
What happens if you don't remove the listener?
Then it will always keep on listening. In an Android context, the listener knows nothing about the Android activity lifecycle. So as said above, so you'll have to explicitly remove it at the right time, so it cannot lead to unexpected results and crashes.
How to remove Firebase EventListener?
Firebase Realtime Database listeners are not automatically removed. They only stop when you explicitly remove them, or when the app gets killed. There is no API to get a list of the active listeners, so you will have to track those yourself.
The earliest you should usually connect your listeners is in onStart
, in which case you should remove them in the onStop
of the activity.
Firebase listeners still listening after activity being finished
Before going to next Activity remove the value event listener
refState.removeEventListener(listenerName)
Try this code
Inside onCreate
DatabaseReference refState = FirebaseDatabase.getInstance().
getReference("/Rooms/"+roomName+"/gameState");
refState.addValueEventListener(stateValueEventListner);
Outside onCreate
private ValueEventListener stateValueEventListner = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try {
String gameState = dataSnapshot.getValue(String.class);
if (gameState.equals("choose_letter")) {
System.out.println("Starting game");
startGame();
}
}
catch(Exception e)
{}
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("Error");
}
});
Remove value event listener
refState.removeEventListener(stateValueEventListner):
Android Firebase keep addListenerForSingleValueEvent updated
If you have enabled offline capabilities, it means that you'll be able to use your app even if it temporarily loses its network connection. So the ValueEventListener
will keep listening on the local stored datas as long as you are offline
. If you want the results to be updated, then you need to go online.
If you want to listen to data only once, then you need to use: addListenerForSingleValueEvent(). If you want to use addValueEventListener
, just don't forget to remove the listener according to the life-cycle of your activity as I have already answered here.
Related Topics
How to Efficiently Parse HTML With Java
Why Doesn't Recyclerview Have Onitemclicklistener()
How to Do a Synchronous Request With Volley
Eclipse/Tomcat: Deploy Doesn't Work Any More (Classnotfoundexception)
Swing Gui Listeners Without Awt
How to Create a Java String from the Contents of a File
Trusting All Certificates Using Httpclient Over Https
How to Get a File'S Media Type (Mime Type)
How to Write Files to External Public Storage in Android So That They Are Visible from Windows
Noclassdeffounderror - Eclipse and Android
Set Imageview Width and Height Programmatically
Where to Store Application Data (Non-User Specific) on Linux
How to Create a Generic Array in Java
What Is a Serialversionuid and Why Should I Use It
What Are the Possible Values of the Hibernate Hbm2Ddl.Auto Configuration and What Do They Do