片段的childfragmentmanager getFragments()在替换后返回null

时间:2017-01-11 09:43:52

标签: android replace fragment fragmentmanager android-nested-fragment

我有MainActivity并且里面有一个FragmentLayout。当使用此代码选择选项卡(如instagram)时,我在该布局中显示不同的片段。

MABaseFragment fragment = mBackStackOfFragment.get(getStackNo(stack)).peek();
ft.replace(R.id.contentFrame, fragment).
currentVisibleFragment = fragment;

在其中一个片段中,我使用父片段的childfragmentmanager创建了Viewpager。我用这段代码创建父片段

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    List asd = getChildFragmentManager().getFragments();
    if(view == null)
    {
        view = inflater.inflate(R.layout.fragment_search, container, false);
        setHasOptionsMenu(false);

        actionBar = MAMainActivity.mainActivity.getSupportActionBar();
        mViewPager = (ViewPager) view.findViewById(R.id.pager);
        mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.pager_title_strip);

        mViewPager.setOffscreenPageLimit(numberOfTabs);
        mSlidingTabLayout.setDistributeEvenly(true);
        pagerAdapter = new MASearchFPA(getContext(), getChildFragmentManager());
        mViewPager.setAdapter(pagerAdapter);

        mSlidingTabLayout.setViewPager(mViewPager);

        mSlidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return getResources().getColor(R.color.colorPrimary);
            }

            @Override
            public int getDividerColor(int position) {
                return getResources().getColor(R.color.colorPrimary);
            }
        });
    }
    return view;
}

我保留每个片段的依据,因为允许用户继续他们离开的地方。它适用于其他没有嵌套片段的片段,但是当用这个嵌套片段应用程序替换当前片段时崩溃

01-11 12:36:08.239 20857-20857/tr.com.meeapps.livyapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: tr.com.meeapps.livyapp, PID: 20857
                                                                        java.lang.IllegalStateException: Fragment no longer exists for key f0: index 0
                                                                            at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:692)
                                                                            at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:215)
                                                                            at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1453)
                                                                            at android.view.View.dispatchRestoreInstanceState(View.java:15637)
                                                                            at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3268)
                                                                            at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3274)
                                                                            at android.view.View.restoreHierarchyState(View.java:15615)
                                                                            at android.support.v4.app.Fragment.restoreViewState(Fragment.java:477)
                                                                            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1135)
                                                                            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
                                                                            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                                                                            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
                                                                            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
                                                                            at android.os.Handler.handleCallback(Handler.java:751)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:154)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

因为我认为在调用replace方法之后,childfragmentmanager的mActive列表为null,并且返回null给Viewpager。

所以我不知道为什么替换后子片段的mActive片段为空。有什么方法可以防止这种行为。 感谢。

修改

我的适配器类

public class MASearchFPA extends FragmentStatePagerAdapter {
    Context context;
    MACategoriesFragment categoriesFragment;
    MAMapFragment mapFragment;
    MAFavoritesFragment favoritesFragment;
    FragmentManager fm;
    public MASearchFPA(Context context, FragmentManager fm) {
        super(fm);
        this.context = context;

        this.fm = fm;
        categoriesFragment = new MACategoriesFragment();
        mapFragment = new MAMapFragment();
        favoritesFragment = new MAFavoritesFragment();
    }

    @Override
    public Fragment getItem(int position) {

        if(position == 0) // if the position is 0 we are returning the First tab
        {
            return categoriesFragment;
        }
        else if(position == 1)             // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
        {
            return mapFragment;
        }
        else
        {
            return favoritesFragment;
        }
    }

    @Override
    public int getCount() {
        return MASearchFragment.numberOfTabs;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return context.getResources().getString(R.string.categories);
            case 1:
                return context.getResources().getString(R.string.map);
            case 2:
                return context.getResources().getString(R.string.favorites);
            default:
                return null;
        }
    }

1 个答案:

答案 0 :(得分:0)

我用show和hide方法解决了问题。因为replace方法会破坏片段子片段的引用。显示和隐藏片段相对解决了我的问题。