Android - onAttach(Context) not called for API 23

前端 未结 7 954
离开以前
离开以前 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:28

    I came up with the same issue. I have a minSdkVersion of 19 and a targetSdkVersion of 23 and I am using fragmentManager in an AppCompatActivity.

    onAttach(Activity activity) is marked as deprecated but I ran into app crashes when I replaced it with onAttach(Context context).

    So instead, I now have both versions present and when I run my app on API22, onAttach(Activity activity) is fired, even though it is marked as deprecated.

    I have not tested it on an Android Marshmallow device, but my understanding is that it would run the onAttach(Context context) version, ignoring the other one.

    Update to take into account @skaar's observation in the comments below: I could now test on API23 and can confirm that running on platform 23 causes both methods (with Activity and Context) to be called. Thanks for the heads up!

提交回复
热议问题