title: RecyclerView doesn't unregister itself from the adapter on orientation change
body: <p>I used to use <code>ListView</code> in my android application and I recently switched to <code>RecyclerView</code> and observed that it introduced some memory leaks on orientation change. On further investigation, the reason became apparent</p>
  
  <p><strong>SETUP</strong></p>
  
  <p>A single <code>activity</code> which hosts a <code>fragment</code>, the instance of which is retained across config changes. The <code>fragment</code> contains a single <code>RecyclerView</code> in its layout file that is populated using a custom <code>adapter</code></p>
  
  <p><strong>DRILLING DOWN</strong></p>
  
  <p>Whenever an <code>Adapter</code> is set for any of those 2 views, they register themselves with the adapter to monitor changes to the data and update on the UI. <code>ListView</code> unregisters itself on config changes by</p>
  
  <pre><code>@Override
  protected void onDetachedFromWindow() {
      super.onDetachedFromWindow();
      ...
  
      if (mAdapter != null &amp;&amp; mDataSetObserver != null) {
          mAdapter.unregisterDataSetObserver(mDataSetObserver);
          mDataSetObserver = null;
      }
  
      ...
  }
  </code></pre>
  
  <p>Unfortunately, <code>RecyclerView</code> doesn't do that</p>
  
  <pre><code>@Override
  protected void onDetachedFromWindow() {
      super.onDetachedFromWindow();
      if (mItemAnimator != null) {
          mItemAnimator.endAnimations();
      }
      mFirstLayoutComplete = false;
  
      stopScroll();
      mIsAttached = false;
      if (mLayout != null) {
          mLayout.onDetachedFromWindow(this, mRecycler);
      }
      removeCallbacks(mItemAnimatorRunner);
  }
  </code></pre>
  
  <p><strong>PROOF</strong></p>
  
  <p>I changed the orientation a good number of times and then took a heap dump, and read it using Eclipse's MAT. I did see that there were a good number of instances of my activity because the <code>RecyclerView</code> instances didn't <code>unregister</code> and they have strong references to my activity!!</p>
  
  <p>Am I missing something? How do you guys make sure that the <code>RecyclerView</code> doesn't leak your activity? </p>
  
  <p><strong>FRAGMENT</strong></p>
  
  <pre><code>public class ExampleFragment extends Fragment {
  
      private ExampleAdapter mAdapter = null;
  
      public static ExampleFragment newInstance() {
          return new ExampleFragment();
      }
  
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
          setupAdapterIfRequired();
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setRetainInstance(true);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          return inflater.inflate(R.layout.fragment_example, container, false);
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          setupRecyclerView(getView());
      }
  
      private void setupAdapterIfRequired() {
          if (mAdapter == null) {
              mAdapter = new ExampleAdapter();
          }
      }
  
      private void setupRecyclerView(View rootView) {
          RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.list);
          recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
          recyclerView.setAdapter(mAdapter);
      }
  }
  </code></pre>