54

I am having trouble using or understanding how popping FragmentTransactions off of the back stack handles the custom animations. Specifically, I expect it to call the "out" animation, but it doesn't seem to.

I have a simple method to handle a fragment transaction (FragmentTransaction) where I add a fragment and apply a custom transition so that it will fade-in/fade-out. I am also adding this to the back stack so that the user can undo that transaction with the back button, essentially navigating to the state before the fragment was added.

protected void changeFragment() { 
    FragmentTransaction ft = fm.beginTransaction(); 
    ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out); 
    ft.add(R.id.fragment_container, new TestFragment()); 
    ft.addToBackStack(null); 
    ft.commit(); 
} 

Everything works great moving forward, but when the user clicks the back button, the transition animations do not reverse. What I expected was that when the fragment got removed, it would use the fade out animation. Instead it seems to pop out (without animation) and then the container seems to fade in. I'm not sure that this is exactly what is happening, but the fragment is definitely not fading out.

My application uses the compatibility library to add fragment support, but I assume this to be applicable to Honeycomb (android-11) as well. Does anyone know if I am just doing something wrong here or if I am just expecting too much? Ideally, I would like to animate the fragments similarly to how Gmail (on the Xoom) does in regards to moving forward by clicking a message and then back by using the back button. Preferably not having to override the back button functionality and keep up with my own fragment state since I could have several "transactions" that I would want to back out of and I am not a fan of re-inventing wheels.

Also asked on the Android Developers Group: http://groups.google.com/group/android-developers/browse_thread/thread/1136a3a70fa0b6e9

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
Kelly Merrell
  • 1,245
  • 1
  • 10
  • 16
  • 3
    I also encountered this issue and submitted a bug report here http://code.google.com/p/android/issues/detail?id=15623&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars – Damian Mar 20 '11 at 00:35
  • Please can you star the above bug report. – Damian Mar 20 '11 at 00:36
  • Starred. Thanks for submitting that as a boog. I was wanting to get another take on it before doing so. – Kelly Merrell Mar 22 '11 at 22:25
  • 5
    ... and the bug has been fixed. The fix came too late for the 3.1 release; it will be in an upcoming release. – Chet Haase May 20 '11 at 03:16
  • @Chet: Could you please post the details of the fix as an answer so we can get this off the Unanswered list? Thank you. – Bill the Lizard Jun 24 '11 at 20:59
  • Was wondering if this is also going to be fixed in the Android compatibly package. Latest version I'm using (revision 3) does not seem to have the fix. – taf Aug 04 '11 at 12:25

3 Answers3

50

I use this:

ft.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out, R.anim.hyperspace_in, R.anim.slide_out);

and the transitions work in reverse when the back button is presses.

29

The bug was fixed in the 3.2 release with the addition of the following new api:

http://developer.android.com/reference/android/app/FragmentTransaction.html#setCustomAnimations(int, int, int, int)

It's to be noted that it has not yet been back-ported to the compatibility library (as mentioned in the bug report).

kajham
  • 1,241
  • 15
  • 19
5

It's a bug, look at bug report 15623. One of the Android project members commented that the fix was too late for release 3.1 but it should make it into the next release.

The same member goes on to say that...

The problem is that the same animations are run on a pop operation as were run to put the fragments in their current places. For example, in the sliding example above, on a forward operation (pushing the old fragment onto the stack and moving the new fragment into view), we slide the old fragment out from the center to the left and slide the new fragment in from the right to the center. When the stack is popped, these same animations are run: the most recent fragment is animated 'out' by sliding it in from the right to the center (after which it disappears, since it's being removed). The old fragment is popped off the stack and animated from teh center to the left ... right off the screen.

Gallal
  • 4,267
  • 6
  • 38
  • 61