Firebase is Calling onChildAdded twice

411
March 11, 2017, at 11:08 AM

I am calling OnChildAdded method inside SearchTextView OnQueryListener which searches and Display Records in Dialog using OnChildAdded method. But when I run the code the OnChildAdded method seems to call inner code twice as the dialog Prompt twice and same for Log Statements too.

Here is the Code of the method:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
  mSearchQuery = query.trim();              
  mDatabaseReference.orderByChild("name").startAt(mSearchQuery).addChildEventListener(new ChildEventListener() {
    @Override
      public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        student = dataSnapshot.getValue(Student.class);
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        EnrollOperationFragment enrollOperationFragment =  EnrollOperationFragment.newInstance(student.getName(),student.getId());
        enrollOperationFragment.show(fragmentManager,ENROLL_OPERATION);}
Answer 1

onChildAdded will be called for each child node matching your query. But also note that you're adding a new child listener each time the user submits the search text. So if they submit twice, you have two listeners and your onChildAdded will get called twice for each child (once for each listener). Then when they submit a search again, it'll add a third listener. And so on.

The solution is to detach the previous listener before attaching a new one. See the documentation for detaching listeners. Or use the code from her for inspiration: How stop Listening to firebase location in android

Rent Charter Buses Company
READ ALSO
alarm manager not working while updating interval time

alarm manager not working while updating interval time

after reading all the QA i didnt get any proper solutionI have 2 problems 1

431
How to automate Keystore generation using the java keystore tool? w/o user interaction

How to automate Keystore generation using the java keystore tool? w/o user interaction

I am trying to automate keystore generation using the Java keystore toolThe command I am using is :

498
Intrumented Test related to PendingIntent.getBroadcast()

Intrumented Test related to PendingIntent.getBroadcast()

I am trying to instrumented test my alarm module

302
Cannot send data using Bluetooth HC-06 - app stops working

Cannot send data using Bluetooth HC-06 - app stops working

I'm trying to create connection between Arduino and Android deviceI'm using:

355