2

In my app, i have 4 buttons, these buttons will create and replace fragment then added it to the backstack.
So for example :

[A] -> [B] -> [C] -> [D]

First fragment A is exist, when i click at button B, fragment A will be replaced with fragment B and added fragment B to the backstack, when i click at button C, fragment B will be replaced with fragment C and added fragment C to the backstack, and so on.

My question is, when button A is clicked again, it will removes fragment A from previous backstack and added it to the last stack, so the image will looks like this:

[B] -> [C] -> [D] -> [A]

Or if i click at button B, the image will looks like this:

[A] -> [C] -> [D] -> [B]

How do i do it? i have try using FragmentManager.popBackStack(indexStack, 0), FragmentManager.popBackStack(Tag, 0) but none of these work.

dome
  • 820
  • 7
  • 20
myazidrija
  • 33
  • 5

3 Answers3

2

Remove Specific fragment from the backstack using FragmentManager

FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove(YourSpecificFragmentHere);
trans.commit();
manager.popBackStack();
Mehul Solanki
  • 1,147
  • 11
  • 15
1

try with POP_BACK_STACK_INCLUSIVE

FragmentManager.popBackStack(Tag, FragmentManager.POP_BACK_STACK_INCLUSIVE);

it should woks

Mohammad Sommakia
  • 1,773
  • 3
  • 15
  • 48
0

Try remove your fragment from backstack and then add it again in different transactions :

FragmentA fragmentA = getFragmentManager().findFragmentByTag("TAG_OF_FRAGMENT_A");
getFragmentManager().beginTransaction().remove(fragmentA).commit();
getFragmentManager().beginTransaction().replace(R.id.containerResId, fragmentA ).commit();
lolo.io
  • 770
  • 5
  • 22