Android (Child)FragmentManager 正确使用子子片段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20676690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Proper use of sub sub fragments with (Child)FragmentManager
提问by prom85
How do I properly use Fragments in Fragments?
如何在 Fragments 中正确使用 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 - - -
- - - - - -
- - - - - -
- - ----------------- - -
- --------------------------------------- -
- -
----------------------------------------------------------
Now in my activity's onCreate
I do following:
现在在我的活动中,onCreate
我执行以下操作:
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);
}
And in my fragments onCreate
I get/create my SubFragment:
在我的片段中,onCreate
我得到/创建了我的子片段:
mSubFragment = getChildFragmentManager().findFragmentByTag(SubFragment.class.getName());
if (mSubFragment == null)
{
mSubFragment = new SubFragment();
getChildFragmentManager().beginTransaction().add(R.id.fragment_sub, mSubFragment, SubFragment.class.getName()).commit();
}
Problem
问题
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?
屏幕旋转后,我的 SubFragment 被添加了两次......如果我使用该活动,FragmentManager
那么它就可以工作......但为什么它不能与ChildFragmentManager
? 当然,Fragment 是一个新的 Fragment,但 Activity 也是一个新的 Fragment,那么为什么它可以与 Activity 一起工作,FragmentManager
而不能与父Fragment 一起工作呢?
In a fragment, I should use the fragments ChildFragmentManager
, shouldn't I?
在片段中,我应该使用片段ChildFragmentManager
,不是吗?
回答by ashakirov
You should add SubFragment
to Fragment
the same way like you add Fragment
to Activity
. I mean adding Fragment
to Activity
should look like:
您应该添加SubFragment
到Fragment
像你添加相同的方式Fragment
来Activity
。我的意思是添加Fragment
到Activity
应该看起来像:
@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();
}
}
Adding SubFragment
to MainFragment
should look like:
添加SubFragment
到MainFragment
应如下所示:
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();
}
}
}
or you can add child fragment to Fragment
in onCreate
method
或者您可以Fragment
在onCreate
方法中 添加子片段