I am facing a very strange issue. I have an Activity, with the ActionBar containing a custom view.
If I load the XML of this custom view like this:
LayoutInflater inflater = LayoutInflater.from(this);
ViewGroup vg = (ViewGroup) inflater.inflate(R.layout.action_search_form,null);
It leaks: the activity is not GC’d. The following fix works (why?):
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
However if I set a OnClickListener on a child view it leaks again:
ImageButton clear = (ImageButton) vg.findViewById(...);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Even if empty
}
});
}
All this happen in a private method of the activity and all views are local variables.
I found a fix but do not understand why it works: The view was simple so I coded it in Java instead of inflating the XML. I suspected it is related to the fact that no context is passed to the LayoutInflater and nothing happens out of the Activity itself, but if someone could help me understand what happened I would greatly appreciate it.
All View classes require a Context parameter. When you are inflating the XML, the context provided to the created View is the same context provided to the LayoutInflater. Your view is retaining a reference to your activity.
The reason why when you use getApplicationContext() and the Activity doesn’t leak is because you are passing a context that is global to the entire lifecycle of the application, not just the activity.
** EDIT **
In regards to why the OnClickListener is also retaining your Activity, it is because of a side effect of using anonymous inner classes. By default, these inline implementations retain a reference to the parent / wrapping class instance. This is so you can call methods of the parent from the implementation of the inner.
For example:
Whether or not you have anything in the method body, the inner class still maintains a reference to the parent class.