How to pop fragment off backstack

Three ways to pop Fragment off BackStack

Simply add any of these lines:

1)

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

2)

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

3)

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

They're all easy ways to pop fragment off Backstack


You can try this way

val fm= parentFragmentManager
fm.popBackStack("group", FragmentManager.POP_BACK_STACK_INCLUSIVE)

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);
    }
});