If I use listener in activity in the following manner:
// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Log.d(TAG, "Value is: " + value);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
Attaching an annonymous listener (event that does not attached to variable), do I still need to remove it?
*I set this on the onStart()
and need it to run until onStop()
/ onDestroy()
When is it nessecery to remove the listener?
If you only want the listener to work while the activity is active, you can detach the listener by invoking the removeEventListener() method on your Firebase database reference. If you attach the listener in your onStart(), then you should detach in onStop().
@Override
protected void onStop() {
super.onStop();
//...
myRef.removeEventListener();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With