Animation of fragments and back stack

I'm having trouble using or understanding how FragmentTransactions from the back stack handles custom animations. In particular, I expect it to trigger an “outside” animation, but that doesn't look like it.

I have a simple fragment transaction processing method (FragmentTransaction), where I add a fragment and apply a transition so that it disappears / disappears. I also add this to the back so that the user can cancel this transaction using the back, essentially going 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 fine, but when the user presses the back button, the transition animation does not change. What I expected was that when the fragment was deleted, it would use the fading animation. Instead, it seems to pop up (without animation), and then the container seems to disappear. I'm not sure that this is exactly what is happening, but the fragment definitely does not disappear.

My application uses a compatibility library to add fragment support, but I assume this applies to Honeycomb (android-11). Does anyone know if I'm just doing something wrong here, or if I'm just expecting too much? Ideally, I would like to animate fragments in the same way that Gmail (on Xoom) does in relation to moving forward by clicking a message and then back using the back button. It is preferable not to have to redefine the back button of the functionality and keep up with my own state of fragments, as I could have several “transactions” that I would like to backtrack, and I'm not a fan of reinventing the wheels.

Also asked in the Android development group: http://groups.google.com/group/android-developers/browse_thread/thread/1136a3a70fa0b6e9

+54
android animation android-fragments transactions transitions
Mar 16 '11 at 15:31
source share
3 answers

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

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

It should be noted that it has not yet been ported to the compatibility library (as indicated in the bug report).

+29
Sep 30 '11 at 20:20
source share

I use this:

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

and transitions work in the reverse order when the back button is pressed.

+49
Jun 08 2018-12-12T00:
source share

This is a bug; see bug report 15623 . One of the Android project participants commented that the fix was too late for version 3.1, but it should be in the next version.

The same member further says that ...

The problem is that animations are triggered by pop operations as they were done to put fragments in their current places. For example, in the above example of sliding, forward (by clicking on an old fragment on the stack and moving a new fragment in the field of view), we are the old fragment from the center to the left and slide the new fragment to the right of the center. When the stack popped up, these same animations: the last fragment is an animated "exit", moving it from the right to the center (after which it disappears because it is deleted). An old fragment jumped from the stack and animated from the center to the left ... right from the screen.

+5
Jul 06 2018-11-11T00:
source share



All Articles