问题描述
如何正确使用片段中的片段?
我(简化的)用例都在遵循,我有一个带有布局片段的活动,而这个片段的片段包含一个子片段...所有片段都手动地添加到父母...
---------------------------------------------------------- - Activity - - - - - - --------------------------------------- - - - Fragment - - - - - - - - ----------------- - - - - - SubFragment - - - - - - - - - - - - - - - - - ----------------- - - - --------------------------------------- - - - ----------------------------------------------------------
现在在我的活动中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); }
,在我的片段中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(); }
问题
屏幕旋转后,添加了两次我的子框架...如果我使用活动的FragmentManager,那么它可以工作...但是为什么它不适用于ChildFragmentManager?当然,片段是一个新片段,但是该活动也是一个新片段,所以为什么它可以与活动的FragmentManager一起使用,而与父母的片段不适合?
?在片段中,我应该使用片段ChildFragmentManager,我不应该?
推荐答案
您应该将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(); } }
将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(); } } }
或者您可以将孩子片段添加到Fragment onCreate方法
中问题描述
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 - - - - - - - - - - - - - - - - - ----------------- - - - --------------------------------------- - - - ----------------------------------------------------------
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); }
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(); }
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?
In a fragment, I should use the fragments ChildFragmentManager, shouldn't I?
推荐答案
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(); } }
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(); } } }
or you can add child fragment to Fragment in onCreate method