Android - onAttach(Context) not called for API 23

前端 未结 7 944
离开以前
离开以前 2020-12-08 05:46

I\'ve updated the SDK to the latest version (API 23) and the onAttach(Activity) method for fragment is deprecated. So instead of using that method, now I\'m usi

7条回答
  •  死守一世寂寞
    2020-12-08 06:34

    I have encountered the same problem. What I have done to resolve it:

    1. Change the argument type of onAttach callback from Activity to Context. For unknown reason, this modification results in the fact that the method onAttach(Context) is not called anymore during the fragment lifecycle. As a consequence, my app was crashing since the code that was in onAttach method has never been executed.

    2. Move the code that was in onAttach method to onCreate one since it gets still executed.

    With this modification, the app turns to functionate as before. No additional import statements were required.

提交回复
热议问题