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

前端 未结 7 1019
再見小時候
再見小時候 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:43

    I have faced the same situation. I have gone through various posts and finally implemented my code in onCreate() block of fragment.Till now i haven't faced any issue and i think it wouldn't be an issue as onCreate is called immediately after onAttach in lifecycle.

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            try {
                listnener = (TilesOnClickListnener)getActivity();
            } catch (ClassCastException e) {
                throw new ClassCastException(getActivity().toString() + " must implement OnArticleSelectedListener");
            }
        }
    
    0 讨论(0)
提交回复
热议问题