DatabaseReference Ref = FirebaseDatabase.getInstance().getReference(Constants.Client + \"/\" + path);
Ref.keepSynced(true);
Ref.addValueEvent
To remove the ValueEventListener, you can then do this:
Remove the anonymity of the listener.
Change the code from this:-
Ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
into this:
ValueEventListener listener= new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Ref.addValueEventListener(listener);
Now you will be able to remove the listener:
@Override
public void onDestroy() {
if (Ref != null && listener != null) {
Ref.removeEventListener(listener);
}
}
You need to remove it, so the listener does not stay running in the other activity lifecycles like onDestroy()