我有一个 Fragment(我称之为 pagerFragment
),它被添加到 backstack 并且是可见的。它包含一个 viewPager
和一个 FragmentPagerAdapter
。 FragmentPagerAdapter
包含(假设)两个 fragment :A 和 B。
第一次添加 fragment 效果很好。
Fragment A 有一个按钮,一旦点击,就会将 Fragment (C) 添加到 backstack。
问题是这样的:如果我添加那个 fragment (C),然后点击返回,pagerAdapter
是空的,我看不到里面的任何 fragment 。
如果我使用 hack,并在 pagerFragment
s onDestroyView()
中销毁子 fragment (A 和 B),这解决了问题,虽然我没有不想使用这个技巧。
任何想法可能是什么问题?
最佳答案
我遇到了同样的问题。我的解决方案很简单:
在 onCreateView 我有:
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getActivity()
.getSupportFragmentManager());
SectionPageAdapter 是这样的:
class SectionsPagerAdapter extends FragmentPagerAdapter {
...
}
将 getSupportFragmentManager 更改为
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
它开始工作了! 希望这会有所帮助。
关于android - 导航回 FragmentPagerAdapter -> fragment 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17672779/