title: How to detach firestore listener
body: <p>I'm new to FireStore. I created a ListenerRegistration to update my Recycler View. I know my implementation may not be perfect, but every time my activity got destroyed, my app throws an error on the lines that are inside this Listener. I don't know why, but mt registration.remove() is not working before on destroy or after finish() activity. Can someone help? </p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
    private ListenerRegistration registration;
    private com.google.firebase.firestore.Query query;
  
    private void requestPacienteList(){
  
      FirebaseFirestore db = FirebaseFirestore.getInstance();
      progress.setVisibility(View.VISIBLE);
      query = db.collection("Hospital");
      registration = query.addSnapshotListener(new EventListener&lt;QuerySnapshot&gt;() {
  
          @Override
          public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
              for(DocumentSnapshot documentSnapshot : documentSnapshots){
  
                  if(documentSnapshot.get("nome").equals("Santa Clara")){
  
                      hospital = documentSnapshot.toObject(Hospital.class);
                      hospital.setHospitalDocumentKey(documentSnapshot.getId());
                      FirebaseFirestore db = FirebaseFirestore.getInstance();
  
                      db.collection("Hospital")
                              .document(hospital.getHospitalDocumentKey())
                              .collection("Pacientes")
                              .addSnapshotListener(new EventListener&lt;QuerySnapshot&gt;() {
  
                                   @Override
                                  public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
  
                                      homeModelList.clear();
                                      for(DocumentSnapshot documentSnapshot : documentSnapshots){
  
                                          final Paciente paciente = documentSnapshot.toObject(Paciente.class);
                                          paciente.setPacienteKey(documentSnapshot.getId());
                                          FirebaseFirestore db = FirebaseFirestore.getInstance();
                                          db.collection("Pessoa")
                                                  .document(paciente.getProfissionalResponsavel())
                                                  .addSnapshotListener(new EventListener&lt;DocumentSnapshot&gt;() {
  
                                                      @Override
                                                      public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) {
  
                                                          Profissional profissional = documentSnapshot.toObject(Profissional.class);
                                                          int[] covers = new int[]{R.drawable.ic_person_black};
                                                          HomeModel p = new HomeModel(paciente.getNome()+" "+paciente.getSobrenome(),paciente.getBox(),paciente.getLeito(),
                                                          covers[0],profissional.getNome()+ " "+profissional.getSobrenome(),paciente.getPacienteKey());
                                                          homeModelList.add(p);
                                                          homeAdapter.notifyDataSetChanged();
                                                          prepareListaPacientes();
                                                      }
                                          });
                                      }
                                  }
                              });
                      }
              }
          }
      });
  
      switch (id){
  
          case R.id.logout:
  
              if(FirebaseAuth.getInstance().getCurrentUser()!=null)
                  FirebaseAuth.getInstance().signOut();
  
              Intent it = new Intent(HomeActivity.this, MainActivity.class);
              startActivity(it);
  
              if(registration!=null)
                  registration.remove();
  
              finish();
  
              drawerLayout.closeDrawers();
              break;
      }
    }
  
  }
  </code></pre>
  
  <blockquote>
    <p>My onDestroy method:</p>
  </blockquote>
  
  <pre><code>@Override
  protected void onDestroy() {
      super.onDestroy();
      registration.remove();
  }
  </code></pre>
  
  <blockquote>
    <p>When I remove this if:</p>
  </blockquote>
  
  <pre><code>if(FirebaseAuth.getInstance().getCurrentUser()!=null)
      FirebaseAuth.getInstance().signOut();
  </code></pre>
  
  <p>My problem is gone. But if I don't, I get the following error:</p>
  
  <blockquote>
    <p>java.lang.NullPointerException: Attempt to invoke virtual method
    'java.lang.Object
    com.google.firebase.firestore.DocumentSnapshot.toObject(java.lang.Class)'
    on a null object reference
                                                                    at santauti.app.Activities.Home.HomeActivity$4$1$1.onEvent(HomeActivity.java:200)
                                                                    at santauti.app.Activities.Home.HomeActivity$4$1$1.onEvent(HomeActivity.java:197)
                                                                    at com.google.firebase.firestore.DocumentReference.zza(Unknown Source)
                                                                    at com.google.firebase.firestore.zzd.onEvent(Unknown Source)
                                                                    at com.google.android.gms.internal.zzejz.zza(Unknown Source)
                                                                    at com.google.android.gms.internal.zzeka.run(Unknown Source)
                                                                    at android.os.Handler.handleCallback(Handler.java:751)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                    at android.os.Looper.loop(Looper.java:154)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)</p>
  </blockquote>