区分静态片段和动态片段
Distinguishing static fragments from dynamic ones
正在编写一个 android 库,我正在使用 supportFragmentManager.registerFragmentLifecycleCallbacks
监听片段的生命周期。回调中有片段对象,如何检查片段是静态片段(在 xml 文件中定义)还是动态片段(在 FrameLayout 等容器中定义)?
我自己找到了解决方案,实际上很简单。
片段 class 有一个名为 isInLayout()
的方法,如果片段是使用 <fragment>
标签创建的,则 returns 为真。
这是该方法的文档:
public final boolean isInLayout ()
Return true if the layout is included as part of an activity view hierarchy via the <fragment>
tag. This will always be true when fragments are created through the <fragment>
tag, except in the case where an old fragment is restored from a previous state and it does not appear in the layout of the current state.
正在编写一个 android 库,我正在使用 supportFragmentManager.registerFragmentLifecycleCallbacks
监听片段的生命周期。回调中有片段对象,如何检查片段是静态片段(在 xml 文件中定义)还是动态片段(在 FrameLayout 等容器中定义)?
我自己找到了解决方案,实际上很简单。
片段 class 有一个名为 isInLayout()
的方法,如果片段是使用 <fragment>
标签创建的,则 returns 为真。
这是该方法的文档:
public final boolean isInLayout ()
Return true if the layout is included as part of an activity view hierarchy via the
<fragment>
tag. This will always be true when fragments are created through the<fragment>
tag, except in the case where an old fragment is restored from a previous state and it does not appear in the layout of the current state.