如何清除片段backstack直到指定的片段?

时间:2016-03-05 19:20:17

标签: android android-fragments android-viewpager fragmentstatepageradapter fragment-backstack

我正在开发一个带有viewpager选项卡的应用程序。我正在使用fragmentstatepageradapter来处理viewpager选项卡。我使用childFragmentmanager用嵌套片段替换第一个片段,并将当前片段添加到backstack。控制流程如下。

第一个标签片段A - >替换为片段B - >替换为片段C - >替换为片段D。

现在当我点击片段D中的特定按钮时,我需要回到片段B,从后台堆栈中清除碎片C,D。

我试过

getSupportFragmentManager().popBackStack(
    getSupportFragmentManager().getBackStac‌​kEntryAt(0).getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE); 

但不成功。有人能告诉我清除背板并从片段D导航到片段B的正确方法吗? 这是我的fragmentstatepageradapter

public class HomeViewPagerAdapter extends FragmentStatePagerAdapter {

    CharSequence Titles[]; // This will Store the Titles of the Tabs which are
    Context mcontext;                        // Going to be passed when ViewPagerAdapter is
    int[] images;                        // created
    int NumbOfTabs;
    private Fragment mFragmentAtPos0;
    SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
    Fragment homefragment;
    Context context;
    FragmentManager fragmentmanager;

    public HomeViewPagerAdapter(Context context, FragmentManager fm,
                                CharSequence mTitles[], int mNumbOfTabsumb) {
        super(fm);
        this.Titles = mTitles;
        this.NumbOfTabs = mNumbOfTabsumb;
        this.context = context;
        this.fragmentmanager = fm;
    }

    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub

        if (position == 0) 
        {
            SharedPreferences LoginPref = this.context.getSharedPreferences("login_credentials", Context.MODE_PRIVATE);
            if (LoginPref.contains("login_pref") && LoginPref.getString("login_pref", "").equals("loggedin")) {
                homefragment = new SearchHomeFragment();
            } else {
                homefragment = new HomeScreenFragment();
            }
            return homefragment;
        } else if (position == 1) 
        {
            HomeCategoriesFragment homecategoriesfragment = new HomeCategoriesFragment();
            return homecategoriesfragment;

        } else if (position == 2) 
        {
            HomePriceComparisonFragment homepricecomparefragment = new HomePriceComparisonFragment();
            return homepricecomparefragment;
        } else if (position == 3) 
        {
            HomeEnterFeedFragment homeenterfeedfragment = new HomeEnterFeedFragment();
            return homeenterfeedfragment;
        } else {
            HomeMoreFragment homemorefragment = new HomeMoreFragment();
            return homemorefragment;
        }

    }

    @Override
    public int getItemPosition(Object object) {
        Log.e("getItemPosition", "getItemPosition");
        if (object instanceof HomeScreenFragment)
            return POSITION_NONE;
        else
            return POSITION_UNCHANGED;

    }

    @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);
        if(MyTabFragment.shouldDestroy) {
            super.destroyItem(container, position, object);
            MyTabFragment.shouldDestroy=false;
        }
    }

    public void resetScreendata() {
        if (homefragment != null) {
            homefragment.getChildFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }
    public void resettoSearch() {

        if (homefragment != null) {
            homefragment.getChildFragmentManager().popBackStack("search", FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }
    public Fragment getRegisteredFragment(int position) {
        return registeredFragments.get(position);
    }
    @Override
    public CharSequence getPageTitle(int position) {
        // TODO Auto-generated method stub
        return Titles[position];
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return NumbOfTabs;
    }


}

这就是我替换片段的方式。

final SearchHomeFragment newFragment = new SearchHomeFragment();
FragmentTransaction transaction =getChildFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_right,R.anim.slide_out_left,R.anim.slide_in_left,R.anim.slide_out_right);
transaction.replace(R.id.fragment_mainLayout, newFragment);
transaction.addToBackStack("search");
transaction.commit();

2 个答案:

答案 0 :(得分:1)

在向backstack添加片段时,请为其命名。

fragmentTransaction.addToBackStack("fragmentA");
fragmentTransaction.addToBackStack("fragmentB");
fragmentTransaction.addToBackStack("fragmentC");
fragmentTransaction.addToBackStack("fragmentD");

然后在按钮点击上添加此代码:

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    FragmentManager fm = getActivity()
            .getSupportFragmentManager();
    fm.popBackStack ("fragmentB", FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }
});

答案 1 :(得分:0)

看起来你的代码正在从片段堆栈中弹出所有。尝试使用片段B的特定名称或ID,而不是使用堆栈中第一个片段的id。