问题描述
如何在片段中正确使用片段?
How do I properly use Fragments in Fragments?
下面是我的(简化的)用例,我有一个带有布局片段的活动,该片段本身包含一个子片段...所有片段都手动添加到其父级中...
My (simplified) use case is following, I have an activity with a layout fragment and this fragment theirself contains a sub fragment... all fragments are added manually to their parents...
----------------------------------------------------------
- Activity -
- -
- -
- --------------------------------------- -
- - Fragment - -
- - - -
- - ----------------- - -
- - - SubFragment - - -
- - - - - -
- - - - - -
- - ----------------- - -
- --------------------------------------- -
- -
----------------------------------------------------------
现在我在活动的onCreate
中执行以下操作:
Now in my activity's onCreate
I do following:
if (savedInstanceState == null)
{
// I create the fragment
mMainFragment = new MainFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_main, mMainFragment);
transaction.commit();
}
else
{
// I retrieve the fragment
mMainFragment = (BaseFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_main);
}
在片段onCreate
中,我获取/创建了我的SubFragment:
And in my fragments onCreate
I get/create my SubFragment:
mSubFragment = getChildFragmentManager().findFragmentByTag(SubFragment.class.getName());
if (mSubFragment == null)
{
mSubFragment = new SubFragment();
getChildFragmentManager().beginTransaction().add(R.id.fragment_sub, mSubFragment, SubFragment.class.getName()).commit();
}
问题
屏幕旋转后,我的SubFragment被添加了两次...如果我使用活动的FragmentManager
,那么它可以工作...但是,为什么它不适用于ChildFragmentManager
?当然,Fragment是一个新的片段,但活动也是一个新的片段,那么为什么它可以与活动的FragmentManager
一起使用,而不与父片段的一起使用?
After screen rotation, my SubFragment is added twice... If I use the activity's FragmentManager
then it works... But why does it not work with the ChildFragmentManager
? Of course, the Fragment is a new fragment, but the activity is a new one as well, so why does it work with the activity's FragmentManager
but not with the parent fragment's one?
在一个片段中,我应该使用片段ChildFragmentManager
,不是吗?
In a fragment, I should use the fragments ChildFragmentManager
, shouldn't I?
推荐答案
您应将SubFragment
添加到Fragment
,就像将Fragment
添加到Activity
一样.我的意思是将Fragment
添加到Activity
应该如下:
You should add SubFragment
to Fragment
the same way like you add Fragment
to Activity
. I mean adding Fragment
to Activity
should look like:
@Override
public void onCreate(Bundle savedInstanceState) {
....
if (savedInstanceState == null){
//add fragment
mMainFragment = new MainFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_main, mMainFragment);
transaction.commit();
}
}
将SubFragment
添加到MainFragment
应该如下所示:
Adding SubFragment
to MainFragment
should look like:
public class MainFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater i, ViewGroup c, Bundle savedInstanceState) {
...
if (savedInstanceState == null){
mSubFragment = new SubFragment();
//add child fragment
getChildFragmentManager()
.beginTransaction()
.add(R.id.fragment_sub, mSubFragment, "tag")
.commit();
}
}
}
或您可以在onCreate
方法
这篇关于(Child)FragmentManager正确使用子片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!