问题描述
我有一个 Fragment(我称之为 pagerFragment
),它被添加到 backstack 并且是可见的.它包含一个 viewPager
和一个 FragmentPagerAdapter
.FragmentPagerAdapter
包含(假设)两个片段:A 和 B.
I have a Fragment (I'll call it pagerFragment
) that is added to the backstack and is visible. It holds a viewPager
with a FragmentPagerAdapter
. The FragmentPagerAdapter
holds (let's say) two fragments: A and B.
第一次添加片段效果很好.
First adding of the fragments works great.
Fragment A 有一个按钮,一旦点击,就会将 Fragment (C) 添加到 backstack.
Fragment A has a button that once clicked, adds a fragment (C) to the backstack.
问题是这样的:如果我添加那个片段(C),然后点击返回,pagerAdapter
是空的,我看不到里面的任何片段.
The problem is this: if I add that fragment (C), and then click back, the pagerAdapter
is empty, and I cannot see any fragments inside.
如果我使用 hack,并在 pagerFragment
s onDestroyView()
中销毁子片段(A 和 B),这解决了问题,虽然我没有不想使用这个技巧.
If I use a hack, and destroy the children fragments (A and B) in the pagerFragment
s onDestroyView()
, this solves the problem, although I don't wan't to use this hack.
任何想法可能是什么问题?
Any ideas what the issue could be?
推荐答案
我也遇到了同样的问题.我的解决方案很简单:
I had the same problem. The solution for me was simple:
在 onCreateView 我有:
in onCreateView I had:
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getActivity()
.getSupportFragmentManager());
SectionPageAdapter 是这样的:
where SectionPageAdapter is something like this:
class SectionsPagerAdapter extends FragmentPagerAdapter {
...
}
将 getSupportFragmentManager 更改为
after changing getSupportFragmentManager to
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
它开始工作了!希望这会有所帮助.
it started working! Hope this helps.
这篇关于导航回 FragmentPagerAdapter ->片段为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!