导航回 FragmentPagerAdapter ->碎片是空的 [英] Navigating back to FragmentPagerAdapter -> fragments are empty

查看:36
本文介绍了导航回 FragmentPagerAdapter ->碎片是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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.

片段 A 有一个按钮,一旦点击,就会将片段 (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 pagerFragments 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 ->碎片是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆