title: Where in a Fragment should I unregister a BroadcastReceiver?
body: <p>I have a Fragment with a ListView and a BroadcastReceiver that updates the ListView when new data arrives. The Fragments life is controlled by a ViewPager.</p>
  
  <p>In regard to where to (un)register the BroadcastReceiver, I found <a href="https://stackoverflow.com/a/16617831/476074">several</a> <a href="https://stackoverflow.com/a/10780412/476074">places</a> suggesting to do it in</p>
  
  <p><code>onResume()</code>:</p>
  
  <pre><code>refreshReceiver = new RefreshReceiver();
  LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
          refreshReceiver,
          refreshIntentFilter);
  </code></pre>
  
  <p><code>onPause()</code>:</p>
  
  <pre><code>LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(refreshReceiver);
  </code></pre>
  
  <p>However, this does not work properly. When I long-press the home button to get to the <em>Recent apps</em> screen, <code>onPause()</code> is called. When new information comes in while I'm on <em>Recent apps</em>, the ListView misses the update and shows old information after I go back there.</p>
  
  <p>Now I was thinking about moving the unregistering to the <code>onStop()</code> method (or even in <a href="https://stackoverflow.com/a/4805733/476074"><code>onDestroy()</code></a>), but is that guaranteed to be called when the fragment is destroyed? I was worried because if the BroadcastManager holds a reference to the BroadcastReceiver and that in turn holds a reference to the Fragment, it would be quite a serious memory leak.</p>