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
?
Somehow I have the feeling that fragments shouldn't contain a reference to their Activity,
They already do. Otherwise, the getActivity()
method could not work.
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?)
onAttach()
will be called again, for the new activity, so while you will very briefly leak memory, I would not worry about it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With