onAttach()的片段不叫 [英] onAttach() not called in Fragment

本文介绍了onAttach()的片段不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的片段不叫 onAttach(上下文)方法,当它从 AppCompatActivity 启动。

My Fragment doesn't call onAttach(context) method when it launched from AppCompatActivity.

片段XML创建:

<fragment
    android:id="@+id/toolbar"
    class="package.MainToolbarFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:layout="@layout/fragment_main_toolbar" />

但是,如果我从 support.v4.Fragment onAttach其扩展(上下文)来电!

这可能是什么问题?

当然,我可以扩展从 v4.Fragment 所有片段,但我不希望它。它是不好的做法?
同时项目分SDK 14。

Of course, I can extend all fragments from v4.Fragment, but I don't want it. Is it bad practice? Also project min sdk 14.

推荐答案

这不叫,因为这种方法已在API 23.添加如果您用API 23(棉花糖)您的设备上的应用程序,然后 onAttach(上下文)将被调用。在所有previous的Andr​​oid版本 onAttach(活动)将被调用。

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.

<一个href=\"http://developer.android.com/reference/android/app/Fragment.html#onAttach(android.app.Activity)\">http://developer.android.com/reference/android/app/Fragment.html#onAttach(android.app.Activity)

支持库片段是独立的平台。因此,它适用于所有版本的API。

Support libraries fragment is platform independent. Hence it works on all API versions.

这篇关于onAttach()的片段不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆