Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove previous back stack entry from FragmentManager?

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?

like image 830
nemezis Avatar asked Jan 30 '14 08:01

nemezis


People also ask

How do I remove fragments from FragmentManager?

Answers. The FragmentManager class provides the PopBackStack which could help to clear the back stack. Try getting the count of back stack, then traverse this collection to remove the clear the stack.

How do you delete Backstacks on Android?

Use finishAffinity() to clear all backstack with existing one. Suppose, Activities A, B and C are in stack, and finishAffinity(); is called in Activity C, - Activity B will be finished / removing from stack. - Activity A will be finished / removing from stack. - Activity C will finished / removing from stack.


1 Answers

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

like image 96
Jaiprakash Soni Avatar answered Sep 19 '22 05:09

Jaiprakash Soni