Wasted two hours on this problem. Tried everything but none of the solutions worked.
I used data read code of firebase onChildAdded to get the user data then determine it using if else condition.
In onCreate
mFirebaseDatabase=FirebaseDatabase.getInstance();
mMessagesDatabaseReference=mFirebaseDatabase.getReference().child("user").child(root).child(id);
attachDatabaseReadListener();
Listener method
private void attachDatabaseReadListener() {
if ( mChildEventListener == null) {
mChildEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
User u = dataSnapshot.getValue(User.class);
mUser=u.GetUser();
mEmail=u.GetEmailId();
Toast.makeText(getApplicationContext(),"here"+mUser,Toast.LENGTH_SHORT).show();
}
public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
public void onChildRemoved(DataSnapshot dataSnapshot) {}
public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
public void onCancelled(DatabaseError databaseError) {}
};
mMessagesDatabaseReference.addChildEventListener( mChildEventListener);
}
}
Condition check, It can be anything.
if(mUser==null&&mEmail==null) {
//do something here
}
else
{
//do nothing
}
Note: some time is required to execute on child added so add proper delay
I have only one Node so this code worked perfectly for me. If any other solution or easier solution is available please let me know.
if root node doesn't exists then also it will show null so keep that in mind before deploying application push root node.