I am new to this SoftReference and WeakReference stuff. I am making a generic ViewHolder
pattern which keeps all views in Hashtable
keyed by View's Id. I decided to use SoftReference for Hashtable and also SoftReference for value of item inside Hashtable.This is my ViewHolder class
public class ListItemTagHolder {
private int index;
private SoftReference<java.util.Hashtable<Integer,SoftReference<View>>> views=
new SoftReference<java.util.Hashtable<Integer,SoftReference<View>>>(
new java.util.Hashtable<Integer,SoftReference<View>>());
public ListItemTagHolder(int index){
this.index=index;
}
public void destroy(){
java.util.Hashtable<Integer,SoftReference<View>> vals=views.get();
Enumeration<Integer> ks=vals.keys();
while(ks.hasMoreElements()){
vals.get(ks.nextElement()).clear();
}
vals.clear();
views.get().clear();
views.clear();
ks=null;
vals=null;
}
public View getView(int id){
return views.get().get(id).get();
}
public void addView(View v){
views.get().put(v.getId(),new SoftReference<View>(v));
}
public int getIndex(){
return index;
}
public void setIndex(int index){
this.index=index;
}
}
I want to know the following
Is it correct usage of SoftReference ?
What will happen if my activity is destroyed and I am unable to call
destroy()
method ofListItemTagHolder
. Will the memory get cleaned up for Hashtable ?Will all the items in Hashtable be available to me everytime unless all reference to ListItemTagHolder are removed ?