Getting a reference to a child Fragment after the

2020-05-17 08:35发布

Starting Android 4.2, Android supports nested Fragments. The doc doesn't give a lot of explanations regarding nested Fragment lifecycles but from experience, it appears their lifecycle is really similar to "regular" Fragments.

It looks like there is one big difference though: child Fragments are not restored when the parent Fragment onCreate method is called. As a consequence, it seems impossible to save/restore a reference to a particular Fragment:

  • Using getChildFragmentManager.findFragmentByTag(String) always returns null in parent Fragment onCreate(Bundle) because mActive is null.
  • Using putFragment/getFragment results in a NullPointerException because getFragment looks for the size of a null mActive ArrayList.

So, my question is quite simple. Is there a correct way to retrieve a reference to a child Fragment in the parent Fragment onCreate method?

3条回答
够拽才男人
2楼-- · 2020-05-17 08:46

What about setRetainInstanceState(true) on your fragment? Could it solve your problem? It solved some problems when I have ChildFragments in a Fragment. I only have to keep a reference to the childfragment in the fragment.

But I allways did that in onCreateView(). Not sure if it will work in onCreate()

Or do you mean something completely different?

查看更多
Evening l夕情丶
3楼-- · 2020-05-17 09:00

I don't think you can in onCreate as the view isn't constructed at that time. You can in onViewCreated() though. The logic I used is:

  • Check if there is saved state in onViewCreated(), if there is, try to get the child fragment
  • Then check if the child fragment is null, if it is, add it using the child fragment manager.

By "checking" I mean looking up the fragment by id. I guess by tag should work too.

AFAIK you can't get a child fragment before the view hierarchy is restored or created, but you could do the same at later time, for example in onActivityCreated()

查看更多
爷的心禁止访问
4楼-- · 2020-05-17 09:11

are u using FragmentPagerAdapter? if not try FragmentPagerAdapter instead of FragmentStatePagerAdapter I realised that have some bug when using FragmentStatePagerAdapter when i have 4 level nest. Sorry my english is poor.

@Override
public void onCreate(Bundle savedInstanceState) {
    if (savedInstanceState != null) {
     mFragment1 = getFragmentManager().getFragment(savedInstanceState, STATE_Fragment1);
     mFragment2 = getFragmentManager().getFragment(savedInstanceState, STATE_Fragment2);
     mFragment3 = getFragmentManager().getFragment(savedInstanceState, STATE_Fragment3);
    } else {
     mFragment1 = SomeFragment.newInstance("param1");
     mFragment2 = SomeFragment.newInstance("param2");
     mFragment3 = SomeFragment.newInstance("param3");
    }
    super.onCreate(savedInstanceState);
    mMyPagerAdapter = new MyPagerAdapter(getChildFragmentManager(), mFragment1, mFragment2, mFragment3);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    if (mFragment1 != null) {
        getFragmentManager().putFragment(outState, STATE_Fragment1,
                mFragment1);
    }

    if (mFragment2 != null) {
        getFragmentManager().putFragment(outState, STATE_Fragment2,
                mFragment2);
    }

    if (mFragment3 != null) {
        getFragmentManager().putFragment(outState, STATE_Fragment3,
                mFragment3);
    }

    super.onSaveInstanceState(outState);
}
查看更多
登录 后发表回答