I have 3 fragments in my app: [a]->[b]->[c]. first fragment a is added, then it is replaced with b and then b is replaced with c. when the user is in fragment b he can go back to fragment a by pressing back button, but when in fragment c, fragments a and b must not be accessible with back button (pressing back button must close all fragments).
I have tried using
getSupportFragmentManager().popBackStackImmediate(name, FragmentManager.POP_BACK_STACK_INCLUSIVE);
but it pops all fragments from the top (current fragment) to the specified fragment which is not good idea. any simple solution?!
I am having a situation Fragment A is used Add but in FragmentB I used replace for FragmentC.
FragmentA--Add---FragmentB---Repace --FragmentC
When i clicked back from fragment C it brings me back to Fragment A.
Note : all fragments added in backstack
Required behavior : When pressed back from fragmentC it must come back to FragmentB. Instead it brings back to fragmentA
Plz help.
Regards
This was asked in one of the interviews. The scenario is described as follows :
MainActivity (initially has Fragment A added to it.). On pressing a button, it goes to Fragment B, without adding A to backstack. On pressing a button in Fragment B, it adds 'B' to back stack, and goes to Fragment C. Now, in fragment C, I have a button through which I directly want to navigate to fragment A (which wasn't added to back stack). Is there any way possible to do this?
I have an activity. When it starts it loads Fragment A in onCreate using replace. This is not added to the back stack.
I then want Fragment B, an overlay, to be added on top of the previous fragment. This is because i still want you to still be able to see Fragment A behind it as Fragment B has a translucent background. This is loaded using add and is added to the back stack.
From Fragment B i load Fragment C using replace and add to the back stack. This is because Fragment C is not an overlay like Fragment B. When i'm in Fragment C i want to go back to Fragment B on back press. I override on back press and pop the back stack.
The issue is when i press the back button i see Fragment A and not Fragment B. Even though i have debugged and can see Fragment B in the back stack. Why is this?
I have a little uncommon fragment navigation, because I want to have app responsive. Due to that I use almost only show/hide methods with fragments.
Whenever I want to navigate to another fragment and go back with the back key, I add this transaction to the backstack. With that, I also set the transition animation to that transaction, so that when a user presses back, it shows a reversal animation (the effect of popBackStackImmiediate()), when user goes to previous fragment.
I add these animations by:
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
Lets call it A -> B -> A navigation. So when A is moved to B, an opening animation is shown, and then when we go back to A, a reversal animation is shown.
However the problem arrises, when I have a situation where I need to go from A to B adding this transaction to back stack (to be able to return to A with back button), and then from B to C (not adding this transaction to back stack), and from C to A when pressing back button. The problem in this scenario is that I want the user to be brought immidietly to A fragment when pressing back button on that C fragment. However, because there was animation added to transaction from A to B, and that transaction was added to back stack, when user presses back button on fragment C, the reversal transaction A->B is being shown (in effect, the fragment B flashes for a fraction of a second on a screen, before fragment A is displayed).
It would all run beautifully if I was able to get to the transaction object in A->B transaction and disable that transition animation. However it seems this operation has no effect after a commit() has been done.
Is there anyway I can disable that animation?
What first comes to mind would be to call popBackStackImmediate() right before going to fragment C. That way when you return from C you go straight to A.
Now about the animation thing, you could try this answer.
It basically says that you create a boolean flag that you set to true when you don't want to see the animations.
Then if the above condition is true, the fragment's onCreateAnimation method you returns an empty animation.