Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will this Android example be safe to use, memory leak-wise?

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?

like image 808
Maarten Avatar asked Nov 02 '22 08:11

Maarten


1 Answers

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.

like image 57
CommonsWare Avatar answered Nov 15 '22 04:11

CommonsWare