Is this a good technique (Using data holder) to eliminate anonymous class, to reduce memory leak risk
<p>Anonymous class can easily cause memory leak, especially in Android world, where Activity or Fragment can suddenly destroyed due to configuration changes. Here are one of many examples.</p>

<p><a href="http://chaosinmotion.com/blog/?p=696" rel="nofollow">http://chaosinmotion.com/blog/?p=696</a></p>

<p><a href="http://blog.andresteingress.com/2011/10/12/anonymous-inner-classes-in-android/" rel="nofollow">http://blog.andresteingress.com/2011/10/12/anonymous-inner-classes-in-android/</a></p>

<p><a href="https://blogs.oracle.com/olaf/entry/memory_leaks_made_easy" rel="nofollow">https://blogs.oracle.com/olaf/entry/memory_leaks_made_easy</a></p>

<p>The reason is that, creating an anonymous class in <code>Activity</code> or <code>Fragment</code>, the anonymous class will always hold an implicit reference to the <code>Activity</code> or <code>Fragment</code>. So, when <code>Activity</code> tends to go out of life due to configuration changes, it cannot be garbage collected if the anonymous class is being exposed and hold by outside world.</p>

<p><strong>So, I was wondering, whether using a data holder technique is a good way, to completely eliminate anonymous class, to reduce risk of memory leakage? Or, am I over paranoid?</strong></p>

<h2>Using anonymous class</h2>

<pre><code>public class HomeMenuFragment {
    private Parcelable selectedInfo = null;
    private List&lt;View&gt; homeMenuRows = new ArrayList&lt;View&gt;();

    private void fun() {
        ...
        ...
        row.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // homeMenuRows is member variable
                for (View r : homeMenuRows) {
                    r.setSelected(false);
                }
                row.setSelected(true);
                // selectedInfo is member variable
                selectedInfo = watchlistInfo;
            }
        });
    }
}
</code></pre>

<h2>Refactor using data holder technique</h2>

<pre><code>public class HomeMenuFragment {
    private static class Holder {
        public Parcelable selectedInfo = null;
        public final List&lt;View&gt; homeMenuRows = new ArrayList&lt;View&gt;();        
    }
    private final Holder holder = new Holder();

    private static class MyOnClickLisnter implements OnClickListener {
        private final Holder holder;
        private final LinearLayout row;
        private final WatchlistInfo watchlistInfo;

        public MyOnClickLisnter(Holder holder, LinearLayout row, WatchlistInfo watchlistInfo) {
            this.holder = holder;
            this.row = row;
            this.watchlistInfo = watchlistInfo;
        }

        @Override
        public void onClick(View arg0) {
            for (View r : holder.homeMenuRows) {
                r.setSelected(false);
            }
            row.setSelected(true);
            holder.selectedInfo = watchlistInfo;
        }        
    }

    private void fun() {
        ...
        ...
        row.setOnClickListener(new MyOnClickLisnter(holder, row, watchlistInfo));
    }
}
</code></pre>
