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

前端 未结 7 1021
再見小時候
再見小時候 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条回答
  •  -上瘾入骨i
    2020-12-14 08:22

    Check the source code:

    /**
     * Called when a fragment is first attached to its context.
     * {@link #onCreate(Bundle)} will be called after this.
     */
    public void onAttach(Context context) {
        mCalled = true;
        final Activity hostActivity = mHost == null ? null : mHost.getActivity();
        if (hostActivity != null) {
            mCalled = false;
            onAttach(hostActivity);
        }
    }
    
    /**
     * @deprecated Use {@link #onAttach(Context)} instead.
     */
    @Deprecated
    public void onAttach(Activity activity) {
        mCalled = true;
    }
    

    So the onAttach(Activity activity) is called by the onAttach(Context context) if there is a host activity. You can use the onAttach(Activity activity) safely.

提交回复
热议问题