title: Removing continuous Firebase listeners on Fragment change
body: <p>Each Fragment in my application has their own set of continuous Firebase listeners. These become unnecessary once the active Fragment has changed, however, the listeners continue in the background. Besides that, these old listeners may trigger View updates for the previous Fragment, which may then crash the application. So, I'd like to be tidy and clean up the listeners once the Fragment changes. I do not know how, but I have an alternative solution and I was curious about your thoughts and suggestions.</p>
  
  <p>The listener functions are called in the Fragments <code>onCreate</code>, for example:</p>
  
  <pre><code>fuSetListener();  
  </code></pre>
  
  <p>These functions retrieve and save the data in global variables.</p>
  
  <p>In the <code>onDataChange</code> of the Firebase listener I perform this if-statement to check if the listener should be removed:</p>
  
  <pre><code>if(getActivity()==null){ 
      reference.removeEventListener(this); 
  } else {
      // Perform necessary onDataChange actions
  }
  </code></pre>
  
  <p>So the question arises, does <code>getActivity()</code> only return <code>null</code> in case the Fragment has changed? Or may <code>getActivity()</code> return <code>null</code> without changing the Fragment and mess up my Fragments purpose?</p>
  
  <p>I have applied this method a couple of times and do not run into trouble yet, but I wanted to check with you before proceeding. </p>