android – 区分静态片段和动态片段

编写一个
android库,我正在使用supportFragmentManager.registerFragmentLifecycleCallbacks监听片段的生命周期.在回调中有片段对象,我如何检查片段是静态片段(在xml文件中定义)还是动态片段(在像FrameLayout这样的容器中定义)? 最佳答案 我自己找到了解决方案,实际上这很简单.

Fragment类有一个名为isInLayout()的方法,如果片段是使用< fragment>创建的,则返回true.标签.

以下是该方法的文档:

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.

点赞