farooq mughal
farooq mughal

Reputation: 53

OnChildAdded EventListener call every time the Activity is started.even if no new child is added

I am developing a chat app in firebase android. Whenever i close app and open again. onchildadded listener is called. even no new child is being added.This is my code below. this code should only be invoked if new child added again. not for the previous one.Can someone solve this problem?

String uid = mAuth.getCurrentUser().getUid();
DatabaseReference ddr = 
FirebaseDatabase.getInstance().getReference().child("Orders").child(uid);
ddr.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        Intent i = new Intent().setClass(getApplication(), DialogActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
        Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        getApplication().startActivity(i);
    }
});

Upvotes: 1

Views: 432

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

What you're describing is the expected behavior. From the Firebase documentation for `onChildAdded:

Retrieve lists of items or listen for additions to a list of items. This callback is triggered once for each existing child and then again every time a new child is added to the specified path.

Upvotes: 2

Related Questions