How to pop fragment off backstack

限于喜欢 提交于 2019-11-27 03:31:21
Archie.bpgc

You can pop the fragment by name. While adding fragments to the back stack, just give them a name.

fragmentTransaction.addToBackStack("fragB");
fragmentTransaction.addToBackStack("fragC");

Then in Fragment_C, pop the back stack using the name ie.. fragB and include POP_BACK_STACK_INCLUSIVE

someButtonInC.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        FragmentManager fm = getActivity()
                .getSupportFragmentManager();
        fm.popBackStack ("fragB", FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }
});

Three Way To pop Fragment off BackStack

  1. Simply Add This Line

        getActivity().getSupportFragmentManager().popBackStack();
    

2.#

        getActivity().getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

3.#

    getActivity().getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

its all easy way to pop fragment off backdtack

first replacing fragment container_view that time we add name as like "Later Transaction"

   getSupportFragmentManager().beginTransaction().replace(R.id.container_view, 
    profileFragment, "Profile").addToBackStack("Later Transaction").commit();

then on back press button pop the back stack using the Later Transaction name

     int count = getSupportFragmentManager().getBackStackEntryCount();
    if(count > 1) {
     getSupportFragmentManager().popBackStack("Later Transaction", 
     FragmentManager.POP_BACK_STACK_INCLUSIVE);
    } else {
        DialogUtils.show(HomeActivity.this, 
        getString(R.string.exit_app_message), getString(R.string.alert), 
       "Yes","No", new DialogUtils.ActionListner() {
            @Override
            public void onPositiveAction() {
                finish();
            }
            @Override
            public void onNegativeAction() {
            }
        });
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!