How to remove previous back stack entry from FragmentManager?
Asked Answered
A

2

14

I have an activity in which I am switching fragments using following method:

public void setCurrentFragment(Fragment fragment) {
     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
     transaction.setCustomAnimations(R.anim.slide_left_right_in, R.anim.slide_left_right_out, R.anim.slide_right_left_in, R.anim.slide_right_left_out);
     transaction.replace(R.id.contentFrameLayout, fragment, Integer.toString(fragmentId++));
     transaction.addToBackStack(Integer.toString(fragmentId));
     transaction.commit();
}

My navigation stack looks like this:

N-2 -> N-1 -> N

When certain fragment N is 'opened' I want previous one (N-1) to be removed from the backstack, so when I press 'back' I want N-2 fragment to be restored.

When I call FragmentManager.popBackStack(..) in N fragment it removes N and N-1 fragment.

I have tried to call popBackStack(..) in N-1 fragment right before switching to N fragment. But in that case N-2 fragment is resumed, and only after that N fragment is displayed.

My question is: is there any way to remove previous fragment from backstack without popping current fragment?

Anserine answered 30/1, 2014 at 8:13 Comment(1)
Did you got any solution for the same as I am also facing the same issue?Stoplight
A
1

use public abstract void popBackStack (int id, int flags) to back stack. Check this for example getSupportFragmentManager().popBackStack(fragmentId,0);

Approver answered 30/1, 2014 at 9:33 Comment(1)
I have already tried it, it removes my current fragment from backstack too. Documentation says: 'Pop all back stack states up to the one with the given identifier. 'Anserine
I
0

you can use alternate method with above one which will not use addToBackStack() method and call directly commit();

Irretrievable answered 22/1, 2015 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.