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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:16:20  来源:igfitidea点击:

Proper use of sub sub fragments with (Child)FragmentManager

androidfragmentfragmentmanager

提问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 onCreateI 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 onCreateI 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 FragmentManagerthen 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 FragmentManagerbut 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 SubFragmentto Fragmentthe same way like you add Fragmentto Activity. I mean adding Fragmentto Activityshould look like:

您应该添加SubFragmentFragment像你添加相同的方式FragmentActivity。我的意思是添加FragmentActivity应该看起来像:

 @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 SubFragmentto MainFragmentshould look like:

添加SubFragmentMainFragment应如下所示:

    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 Fragmentin onCreatemethod

或者您可以FragmentonCreate方法中 添加子片段