区别 getChildFragmentManager getSupportFragmentManager

The definition of getChildFragmentManager() is:

Return a private FragmentManager for placing and managing Fragments inside of this Fragment.

Meanwhile the definition of getFragmentManager() (or in this case getSupportFragmentManager()) is:

Return the FragmentManager for interacting with fragments associated with this fragment's activity.

Basically, the difference is that Fragment's now have their own internal FragmentManager that can handle Fragments. The child FragmentManager is the one that handles Fragments contained within only the Fragment that it was added to. The other FragmentManager is contained within the entire Activity.

In this case, what I'm guessing is you've added the Fragments to the Activity's FragmentManager. You get the child FragmentManager which doesn't contain what you are looking for. Thus you get the exception because it can't find the Fragment with the given ID because it's in a different FragmentManager.


=============================================

mSectionsPagerAdapter = new SectionsPagerAdapter(getActivity().getSupportFragmentManager());

在Fragment里面嵌套Fragment 的话,不要用上面的那句。。。会在ViewPager中出现。有些Fragment 不会加载的情况。。。既ViewPager 加载 Fragment 空白页的情况。。。。

所以   Fragment里面嵌套Fragment 的话:一定要用getChildFragmentManager();

  mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());



  • 8
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
在面试中,可能会涉及到关于Fragment的一些常见问题,以下是一些可能会被问到的问题及其答案: 1. 什么是Fragment? Fragment是Android中的一种可重用的组件,它可以与Activity进行交互,作为Activity界面的一部分进行展示。它可以有自己的布局和生命周期,并且可以被添加、移除、替换或者在Activity中进行切换。 2. Fragment的生命周期是什么样的? Fragment的生命周期包括以下几个方法:onAttach、onCreate、onCreateView、onActivityCreated、onStart、onResume、onPause、onStop、onDestroyView、onDestroy和onDetach。这些方法提供了在Fragment生命周期中不同阶段执行代码的机会,开发者可以在这些方法中处理相关的逻辑。 3. Fragment和Activity之间的通信方式有哪些? Fragment和Activity之间可以通过接口回调、广播、观察者模式等方式进行通信。其中,接口回调是最常见的方式,Activity可以实现一个接口,Fragment通过该接口将数据传递给Activity。广播和观察者模式可以用于更复杂的场景,可以实现多个Fragment和Activity之间的通信。 4. FragmentPagerAdapter和FragmentStatePagerAdapter有什么区别? FragmentPagerAdapter和FragmentStatePagerAdapter都是ViewPager的适配器,用于管理Fragment的切换。区别在于FragmentPagerAdapter会保留Fragment的状态,而FragmentStatePagerAdapter会销毁之前的Fragment,适用于有大量Fragment的情况。 5. getFragmentManager、getSupportFragmentManager和getChildFragmentManager有什么区别? getFragmentManager是用于获取与当前Fragment关联的FragmentManager,通常用于在Fragment中获取父级Activity的FragmentManager。getSupportFragmentManagerSupport库中的方法,用于获取兼容版本的FragmentManager,通常用于在Support库中使用Fragment。getChildFragmentManager是用于获取Fragment中嵌套的子级FragmentManager,通常用于在一个Fragment中管理另一个Fragment。 以上是一些关于Fragment的常见面试题及其答案。希望对你有所帮助。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值