getParentFragment 返回 null [英] getParentFragment returning null
问题描述
我有一个 Fragment
,它有一个 FrameLayout
.第一个片段 (A) 在其 Framelayout 中加载另一个片段 (B).当我从内部片段 (B) 调用 getParentFragment
时,我得到 null
.这个方法应该如何正确使用?
I have a Fragment
that has a FrameLayout
. This first fragment (A) loads inside its Framelayout another fragment (B). When I call getParentFragment
from inner fragment (B), I get null
. How should this method be used properly?
推荐答案
getParentFragment() 是在 API 级别 17 (Android 4.2) 中引入的.Android 4.2 引入了嵌套片段(包含其他片段的片段)的概念.如果片段的父元素是 Activity,则调用此方法会导致 null.
getParentFragment() was introduced in API level 17 (Android 4.2). Android 4.2 introduced the idea of nested fragments (fragments containing other fragments). Calling this results in null if the fragment has a parent which is an Activity.
看看这个.
如果您正在使用支持库,那么您可以使用 getParent(),可能是您在执行片段事务时需要使用 getChildFragmentManager().请参阅这个
If you are using support library then you can use getParent(), may be you need to use getChildFragmentManager() while doing fragment transaction. See this
这篇关于getParentFragment 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!