title: RecylerView Adapter causing memory leak when using on Click interface
body: <p>In my <code>RecyclerView.Adapter</code> I have defined this interface:</p>
  
  <pre><code>public interface OnItemClickListener {
      void onItemClick(View view, int position);
  }
  </code></pre>
  
  <p>The Fragment which contains the <code>RecyclerView</code> implements this interface.
  After 4 or 5 orientation changes <strong>LeakCanary</strong> reports a memory leak:</p>
  
  <p><img src="https://i.stack.imgur.com/tzs06.png" alt=""></p>
  
  <p>My <code>Fragment</code> looks like this:</p>
  
  <pre><code>public class ImagesFragment extends Fragment implements ImageAdapter.OnItemClickListener {
  
      private static final String IMAGES_FRAGMENT_TAG = "ImagesFragment";
      private int SPAN_COUNT = 2;
  
      private String categoryName;
      private String categoryURL;
      private int ImageCount;
  
      protected RecyclerView mRecyclerView;
      protected RecyclerView.LayoutManager mLayoutManager;
  
      protected static ImageAdapter mAdapter;
  
      @Override
      public void onItemClick(View view, int position) {
          Toast.makeText(mContext, "Clicked:" + String.valueOf(position), Toast.LENGTH_SHORT).show();
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          Bundle args = getArguments();
  
          this.categoryName = args.getString("category");
          this.categoryURL = args.getString("URL");
          this.ImageCount = args.getInt("Count");
  
          setRetainInstance(true);
      }
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          View rootView = inflater.inflate(R.layout.images_recycler_view, container, false);
          rootView.setTag(IMAGES_FRAGMENT_TAG);
  
          mRecyclerView = (RecyclerView) rootView.findViewById(R.id.imagesRecyclerView);
  
          mRecyclerView.addItemDecoration(new CategoryItemDecoration(px, SPAN_COUNT, mCurrentLayoutType));
  
          mLayoutManager = new GridLayoutManager(getActivity().getApplicationContext(), SPAN_COUNT);
          mRecyclerView.setLayoutManager(mLayoutManager);
  
          mAdapter = new ImageAdapter(categoryURL, this.ImageCount, this);
          mRecyclerView.setAdapter(mAdapter);
  
          return rootView;
      }
  
      @Override
      public void onSaveInstanceState(Bundle outState) {
          outState.putString(IMAGES_FRAGMENT_TAG, IMAGES_FRAGMENT_TAG);
          super.onSaveInstanceState(outState);
      }
  
      @Override
      public void onDestroyView() {
          super.onDestroyView();
          System.out.println("OnDestroyView");
          if (mRecyclerView != null) {
              mRecyclerView.setAdapter(null);
          }
      }
  
  
      @Override
      public void onDestroy() {
          super.onDestroy();
  
          RefWatcher refWatcher = MyApplication.getRefWatcher(getActivity());
          refWatcher.watch(this);
      }
  }
  </code></pre>
  
  <p>My <code>Adapter</code>:</p>
  
  <pre><code>public class ImageAdapter extends RecyclerView.Adapter&lt;ImageAdapter.ImagesViewHolder&gt; {
  
      private static final String IMAGES_FRAGMENT_TAG = "ImagesFragment";
      private Context mContext;
      private static OnItemClickListener onItemClickListener;
  
      private Integer imageCount;
  
      public ImageAdapter(String url, Integer imageCount, OnItemClickListener itemListener) {
          this.ROOTURL = url;
          this.imageCount = imageCount;
          this.onItemClickListener = itemListener;
      }
  
      @Override
      public int getItemCount() {
          return imageCount;
      }
  
      @Override
      public ImagesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          this.mContext = parent.getContext();
  
          View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.images_viewitem, parent, false);
          ImagesViewHolder imagesViewHolder = new ImagesViewHolder(v);
          return imagesViewHolder;
      }
  
      @Override
      public void onBindViewHolder(final ImagesViewHolder holder, int position) {
          Glide.with(mContext)
                  .load("URL")
                  .asBitmap()
                  .centerCrop()
                  .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                  .into(new BitmapImageViewTarget(holder.image) {
                      @Override
                      public void onLoadFailed(Exception e, Drawable errorDrawable) {
                          super.onLoadFailed(e, errorDrawable);
                          Log.e(IMAGES_FRAGMENT_TAG, "on load failed");
                      }
  
                      @Override
                      public void onResourceReady(Bitmap bitmap, GlideAnimation&lt;? super Bitmap&gt; glideAnimation) {
                          super.onResourceReady(bitmap, glideAnimation);
                          holder.image.setImageBitmap(bitmap);
  
                      }
                  });
      }
  
      public interface OnItemClickListener {
          void onItemClick(View view, int position);
      }
  
      public static class ImagesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
          private static final int PALETTE_SIZE = 24;
          CardView cv;
          ImageView image;
          RelativeLayout mImageViewWrapper;
  
          ImagesViewHolder(View itemView) {
              super(itemView);
              cv = (CardView) itemView.findViewById(R.id.imagesCardView);
              image = (ImageView) itemView.findViewById(R.id.images_ImageView);
              mImageViewWrapper = (RelativeLayout) itemView.findViewById(R.id.Images_imageViewWrapper);
  
              itemView.setOnClickListener(this);
          }
  
          @Override
          public void onClick(View v) {
              onItemClickListener.onItemClick(v, this.getLayoutPosition());
          }
      }
  }
  </code></pre>