When reading up on Fragments, I came across this section on communicating with the activity, which contains the following snippet of code:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnArticleSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
}
}
Somehow I have the feeling that fragments shouldn't contain a reference to their Activity, but I don't really know where this intuition comes from.
I feel like the code above can cause a memory leak when setRetainInstance() is set to true, because the Activity may restart on orientation change while the Fragment is retained, containing a reference to the old Activity. (Is this true?)
But will this pattern be safe to use with setRetainInstance() set to false?