Android填坑记之Fragment中onAttach

今天在写ListFragment的demo时,onAttach(Context context)没有被系统回调,后来找到了答案:

It's not called because this method has been added in API 23. If you run your application on a device with API 23 (marshmallow) then onAttach(Context) will be called. On all previous Android Versions onAttach(Activity) will be called.

本来使用的是onAttach(Activity activity),AS提示该函数被deprecated,所以改用了onAttach(Context context)。但是在API低于 23 的设备中不会去调用后者,只会去调用onAttach(Activity)。

解决方法:

用import android.support.v4.ListFragment代替android.app.ListFragment

你可能感兴趣的:(Android)