2

Having A->B->c in back stack, A is at top. At the time it would like to have C to be on top, and keep A, B in the order in the stack. In the requirement it cannot have two C instances in the stack. The problem is if pop the C then all A, B is removed.

What it wants is to have C->A->B now.

Is it possible to achieve this with FragmentManager's api?

Cœur
  • 37,241
  • 25
  • 195
  • 267
lannyf
  • 9,865
  • 12
  • 70
  • 152
  • There is a reason it is called a stack, I don't think there is an API, but depending on your use case, there is always an approach to get it to work !! – varunkr May 11 '16 at 14:46
  • I think it is already answered in this thread https://stackoverflow.com/questions/21665346/android-reorder-fragment-backstack – Nabeel K May 11 '16 at 14:50
  • Maybe creating a new stack with the desired order, or maybe using reflection to modify the actual one? – Nanoc May 11 '16 at 14:59

3 Answers3

1

FragmentManagerdoes not readily support changing the ordering of the backstack or removing items that aren't at the top of the stack.

You may want to consider managing your own version of a Fragment backstack.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
1

put below code inside your activiy

FragmentManager fragmentManager  


//add fragment A 
fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager .beginTransaction();
ft.add(resId, fragmentA);
ft.addToBackstack("tag");
ft.commit();

//add fragmentB
fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager .beginTransaction();
ft.replace(resId, fragmentB);
ft.addToBackstack("tag1");
ft.commit();

//add fragmentC
fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager .beginTransaction();
ft.replace(resId, fragmentC);
ft.addToBackstack("tag2");
ft.commit();

 //then try to take back click of B and C fragment here through interface

//backclick of C
public void backClick()
{
fragmentManager.popBackStack("tag1",0);
}

//back click of B
public void backClick1()
{
fragmentManager.popBackStack("tag",0);
}
Mrugesh
  • 4,381
  • 8
  • 42
  • 84
0

keep fragment hide and if you want show it.

Ashish Patil
  • 374
  • 4
  • 20