onAttach(Activity) deprecated: where I can check if the activity implements callback interface

前端 未结 7 1022
再見小時候
再見小時候 2020-12-14 07:43

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         


        
7条回答
  •  一整个雨季
    2020-12-14 08:23

    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.

提交回复
热议问题