title: Proper way to resume Firebase EventListener
body: <p>So, let's start from the beginning. I have a project that works with realtime <code>Firestore</code> database. I user <code>EventListener</code> to add, update and remove items from the <code>RecyclerView</code>. Code looks like this:</p>
  
  <p>Fragment</p>
  
  <pre><code>public class ActionsFragment extends Fragment implements ActionsFragmentBase {
  
      private Unbinder unbinder;
      private ActionsPresenter presenter;
      private ActionsRecyclerAdapter actionsAdapter;
      private int state;
      private int type;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          presenter = new ActionsPresenter(this);
          View v = inflater.inflate(R.layout.fragment_actions, container, false);
  
          type = this.getArguments().getInt("type");
          setHasOptionsMenu(true);
          unbinder = ButterKnife.bind(this, v);
  
          return v;
      }
  
      @Override
      public void onResume() {
          super.onResume();
          presenter.notifyFragmentStarted(type);
      }
  
      @Override
      public void onPause() {
          super.onPause();
          presenter.removeRegistration();
      }
  
      @Override
      public void onStop() {
          super.onStop();
          presenter.removeRegistration();
      }
  
      ...
  
      public void handleListUpdate(DocumentChange.Type type, int newIndex, int oldIndex, Action action) {
          if (actionsAdapter != null) {
              switch (type) {
                  case ADDED:
                      actionsAdapter.addItem(newIndex, action);
                      break;
                  case MODIFIED:
                      actionsAdapter.updateItem(oldIndex, newIndex, action);
                      break;
                  case REMOVED:
                      actionsAdapter.removeItem(oldIndex);
                      break;
              }
          }
      }
  
      public boolean isListEmpty() {
          return actionsAdapter == null || actionsAdapter.getItemCount() == 0;
      }
  
      public boolean isAdapterExists() {
          return actionsAdapter != null;
      }
  
      @Override
      public void onDestroyView() {
          super.onDestroyView();
          actionsAdapter = null;
          unbinder.unbind();
          presenter.destroy();
      }
  }
  </code></pre>
  
  <p>Presenter</p>
  
  <pre><code>public class ActionsPresenter {
      private ActionsFragmentBase fragment;
      private ActionsModel model;
  
      private Query actionsQuery;
      private ListenerRegistration actionsRegistration;
  
      public ActionsPresenter(ActionsFragmentBase fragment){
          this.fragment = fragment;
          this.model = new ActionsModel();
      }
  
      public void notifyFragmentStarted(int type) {
          switch (type) {
              /* Getting Query depending on type */
          }
  
          setRegistration(type);
      }
  
      private void notifyViewCreated(int state) {
          fragment.showRequiredViews();
  
          switch (state) {
              /* Setting up Views depending on state */
          }
      }
  
      @SuppressWarnings("unchecked")
      public void setRegistration(int type) {
          actionsRegistration = actionsQuery.addSnapshotListener((documentSnapshots, e) -&gt; {
              if (e == null) {
                  for (DocumentChange dc : documentSnapshots.getDocumentChanges()) {
                      DocumentSnapshot doc = dc.getDocument();
                      Action action;
  
                      /* Getting Action object depending on type */
  
                      if (fragment != null) {
                          if (!fragment.isAdapterExists() &amp;&amp; fragment.isListEmpty()) {
                              fragment.setupLayouts(true, true);
                              notifyViewCreated(Globals.FragmentState.STATE_CONTENT);
                          }
  
                          fragment.handleListUpdate(dc.getType(), dc.getNewIndex(), dc.getOldIndex(), action);
                      }
                  }
  
                  if (fragment != null &amp;&amp; fragment.isListEmpty()) {
                      fragment.setupLayouts(true, false);
                      notifyViewCreated(Globals.FragmentState.STATE_NO_DATA);
                  }
              } else {
                  if (fragment != null) {
                      fragment.setupLayouts(false, false);
                      notifyViewCreated(Globals.FragmentState.STATE_NO_INTERNET_CONNECTION);
                  }
              }
          });
      }
  
      ...
  
      public void removeRegistration() {
          actionsRegistration.remove();
      }
  
      public void destroy() {
          removeRegistration();
          fragment = null;
          model = null;
      }
  }
  </code></pre>
  
  <p>The problem is that my <code>RecyclerView</code> items duplicate on fragment resume. I have no idea how to resume <code>EventListener</code> and not to recreate entire <code>RecyclerView</code> on <code>Fragment</code> resume. Thanks in advance.</p>
  
  <p><strong>UPD</strong></p>
  
  <p>I've just checked official samples and found that every time <code>Activity</code> or <code>Fragment</code> stops they clear <code>RecyclerView</code> items. Of course it works, but I don't really want to reload entire list on fragment resume. But it can be used like temporary workaround.</p>