Viewpager没有保存片段状态

时间:2015-04-07 18:48:13

标签: java android

首先,我要感谢stackoverflow社区。谢谢你,我解决了很多问题!通常我把头撞到墙上几个小时,我在这里找到了解决方案(98%的时间)。这次我想分享一下我的问题。

这是我正在开发的第一款应用。我有一项活动(这很糟糕吗?)。我的app结构如下:Nav抽屉包含四个项目 - 两个viewpage和两个静态片段。 viewpagers包含包含数据的列表,如果单击列表中的某个项目,则会显示详细信息页面,该页面同样是viewpager。当您在详细信息页面上时,可以单击并前进到其他片段。问题是,当我前进到另一个片段onSavedInstance时,没有为viewpagerdetails调用,我无法保留片段状态。如果我传递给FragmentPagerAdapter getFragmentManager()而不是getChildFragmentManager(),将调用onSavedInstance,但如果我在导航回来时执行此操作,则viewpager项为空。我尝试过使用FragmentStatePagerAdapter,但是当我从“其他片段”中单击返回以返回viewpager详细信息时,应用程序崩溃并出现以下错误:关键字f0:index 0的片段不再存在。我已经查看了互联网,但是我没有找到有效的解决方案。有人说问题来自适配器,但我尝试了这个解决方案但它没有用。另外我想手动保留我的片段状态,请不要在应用清单中为我提供配置更改的修复。我试图从onStop方法调用onSavedInstanceState,但是当我返回到片段时,保存的状态为空。我应该在mainActivity中使用静态变量实现它吗?

FragmentPagerAdapter:

public class DetailsSlideAdapter extends FragmentPagerAdapter {
private String[] navMenuTitles;
private FragmentManager manager;
private FragmentTransaction mCurTransaction = null;
private Resources res;

public DetailsSlideAdapter(FragmentManager fm, Resources res) {
    super(fm);
    this.manager = fm;
    navMenuTitles = res.getStringArray(R.array.detailTabs);
    this.res = res;
}

@Override
public int getCount() {
    return 3;
}


@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            DetailsInfo info = new DetailsInfo();
            return info;
        case 1:
            DetailsCast cast = new DetailsCast();
            return cast;
        case 2:
            DetailsOverview overview = new DetailsOverview();
            return overview;
        default:
            return null;
    }

}

@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
            return navMenuTitles[0];
        case 1:
            return navMenuTitles[1];
        case 2:
            return navMenuTitles[2];
        default:
            return navMenuTitles[1];
    }
}

Viewpager详细信息:

    @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // Get the ViewPager and set it's PagerAdapter so that it can display items
    DetailsSlideAdapter = new DetailsSlideAdapter(getChildFragmentManager(), getResources());
    mViewPager = (ViewPager) rootView.findViewById(R.id.DetailsPager);
    mViewPager.setOffscreenPageLimit(3);
    mViewPager.setAdapter(DetailsSlideAdapter);

    // Give the SlidingTabLayout the ViewPager, this must be done AFTER the ViewPager has had
    // it's PagerAdapter set.
    mSlidingTabLayout = (DetailsSlidingTabLayout) rootView.findViewById(R.id.sliding_tabs);
    mSlidingTabLayout.setViewPager(mViewPager);
    mSlidingTabLayout.setSelectedIndicatorColors(getResources().getColor(R.color.tabSelected));
}

如果需要其他代码,我会发布。感谢。

使用FragmentStateAdapter进行编辑: 应用程序崩溃:密钥f0:索引0不再存在碎片。在互联网上,人们说问题来自适配器,但我尝试了这个解决方案但它没有用。代码:

public class DetailsSlideAdapter extends FragmentStatePagerAdapter {
private String[] navMenuTitles;
private FragmentManager manager;
private FragmentTransaction mCurTransaction = null;
private Resources res;
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();


public DetailsSlideAdapter(FragmentManager fm, Resources res) {
    super(fm);
    this.manager = fm;
    navMenuTitles = res.getStringArray(R.array.detailTabs);
    this.res = res;
}

@Override
public int getCount() {
    return 3;
}


@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            DetailsInfo info = new DetailsInfo();
            return info;
        case 1:
            DetailsCast cast = new DetailsCast();
            return cast;
        case 2:
            DetailsOverview overview = new DetailsOverview();
            return overview;
        default:
            return null;
    }

}

@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
            return navMenuTitles[0];
        case 1:
            return navMenuTitles[1];
        case 2:
            return navMenuTitles[2];
        default:
            return navMenuTitles[1];
    }
}


@Override
public Object instantiateItem(ViewGroup container, int position) {
    Fragment fragment = (Fragment) super.instantiateItem(container, position);
    registeredFragments.put(position, fragment);
    return fragment;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    registeredFragments.remove(position);
    super.destroyItem(container, position, object);
}

public Fragment getRegisteredFragment(int position) {
    return registeredFragments.get(position);
}

1 个答案:

答案 0 :(得分:1)

你看到了这个吗? FragmentStatePagerAdapter

来自文档:

  

使用Fragment管理每个的PagerAdapter的实现   页。该类还处理片段的保存和恢复   状态。