I want to remove addValueEventListener listener from a firebase ref when value of particular field is true.
ValueEventListener valueListener=null;
private void
Make sure you add and remove the listener to the same node on your DatabaseReference. For example:
//when declared like this, mDatabaseReference will point to the parent node by default
private DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference();;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//listener added to child node "path_2"
mDatabaseReference.child(path_1).child(path_2).addChildEventListener(myListener);
}
Your listener is this case is pointing to path_2. If you try to remove your listener using this code
//mDatabaseReference pointing to parent node (default behaviour)
mDatabaseReference.removeEventListener(myListener);
it won't work because you are trying to remove the listener from the wrong node. The correct way would be
mDatabaseReference.child(path_1).child(path_2).removeEventListener(myListener);