4

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?

Lucas
  • 3,521
  • 3
  • 27
  • 39
  • You can forego a backstack and override the onbackKey pressed event, and implement your own logic. For instance, back pressed in fragment C, always creates a fragment transaction (with whatever animation you need) to go to fragment A. – NameSpace Nov 01 '14 at 10:07

1 Answers1

1

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.

Community
  • 1
  • 1
Simas
  • 43,548
  • 10
  • 88
  • 116
  • "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." No I don't. If I call this method before I go to fragment C, then upon hitting back button on fragment C, I will exit the application. (Remember that in my example I do not add to the backstack the transaction from B to C). – Lucas Oct 25 '14 at 18:09
  • @Lucas so why don't you add it? – Simas Oct 25 '14 at 18:16
  • Because once you're at C, you cannot go back to B, there is only return to A. Had I add the transaction from B to C, then when hitting back I would go back to B, or if I used the popBachStackImmediete() it would still did not solve my problem, as this reverse animation from A to B would still be there. – Lucas Oct 25 '14 at 22:01
  • @Lucas and did you read the answer I included, which would remove the animation? – Simas Oct 25 '14 at 22:12
  • Sorry! Totaly missed it (idiot). Investigating... give me couple days, I need to test it at work. – Lucas Oct 26 '14 at 08:58
  • Ok I investigated the link you brought as well as couple others that were connected with that. Unfortunetly none of them worked. The overriding of the createAnimation method was promissing, but it doesn't work in my case. Please notice that I do not set custom animations, but a transition through appropriate method. The solutions provided, probably only will work if I set an animation. I tried it anyways and it didn't change anything except the pop in/pop out animations were gone, but the "middle fragment flashback" was still there. – Lucas Oct 28 '14 at 09:34
  • Also keep in mind, if I do not set transition, everything works in order, so there is no "middle fragment flashback". – Lucas Oct 28 '14 at 09:34
  • @Lucas do you override animation or animator? (I meant the method) – Simas Oct 28 '14 at 09:35
  • I was overriding the Fragment method, as in provided links. The method in question: "public Animation onCreateAnimation(int transit, boolean enter, int nextAnim)" – Lucas Oct 28 '14 at 10:55
  • @Lucas the thing is that support fragments have onCreateAnimation while normal frags have onCreateAnimator. The latter doesnt work with animations the way you set them – Simas Oct 28 '14 at 10:59
  • I'm not using Animator directly. Also I'm using support fragments. With those informations would you be kind to rephrase your message? – Lucas Oct 28 '14 at 14:56