Before API 23 I used Fragment\'s onAttach methods to get my listener instance, then the reference is cleaned inside onDetach. ex:
@Override
public void onAtt
I use the native Fragment rather than the one from the Support Library. I put both onAttach()
methods in my code and did some debugging on devices running different SDK versions. I found that:
SDK 22 and below - only onAttach(Activity)
is called. Not surprising really because onAttach(Context) was only introduced in SDK 23.
SDK 23 and above - onAttach(Context)
is called first and then onAttach(Activity)
. (This is consistent with what @Zsolt Mester said about the source code in another answer to this post.)
Since minSdkVersion
for my app was below 23 I just decided to omit onAttach(Context)
altogether and simply add the @SuppressWarnings("deprecation")
annotation to my existing onAttach(Activity)
method.