86

I'm using Google API 8 (Android 2.2) with support package v4.

It doesn't give any error or animation.

Transaction:

FragmentTransaction transaction = manager.beginTransaction();       
transaction.replace(R.id.content, myFragment);
transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
transaction.commit();

Animations:

slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="700"
        android:fromXDelta="-100%"
        android:toXDelta="0%" >
    </translate>
</set>

slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="700"
        android:fromXDelta="0%"
        android:toXDelta="100%" >
    </translate>
</set>

Does anyone know what is happening here?

Lachlan Goodhew-Cook
  • 1,101
  • 17
  • 31
adheus
  • 3,985
  • 2
  • 20
  • 33

5 Answers5

266

The manager was stacking my transaction before I set the animation, so it stacks the transaction without animations (sad but true), and that occurs even if I commit the transaction after the setCustomAnimations().

The solution is to set the animations first:

FragmentTransaction transaction = manager.beginTransaction();       
transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
transaction.replace(R.id.content, myFragment);
transaction.commit();
Sufian
  • 6,405
  • 16
  • 66
  • 120
adheus
  • 3,985
  • 2
  • 20
  • 33
  • so you have to split the statement avoiding the self returned object trick – sherpya Apr 08 '13 at 08:31
  • 5
    `transaction.something().somethingelse().replace().commit()`, many of android methods return `this` so you can avoid retyping the variable, but somehow here exposes a side effect and `setCustomAnimations()` should be called separately – sherpya Sep 18 '13 at 15:55
  • 47
    Just to note, "self returned object trick" is called "method chaining" – Egor Dec 12 '14 at 11:15
  • 1
    Method chaining the above call definitely works. Just tested it in my own project. – MawrCoffeePls Jan 10 '15 at 21:56
  • For additional, to animate fragment when pop from back stack, use `transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right, R.anim.slide_in_left, R.anim.slide_out_right);` instead. – Justin May 04 '15 at 07:35
  • Deserves a +1. That's a life saver for me. It's sad that 5 years later and the issue is not solved... it is called a transaction for a reason !!! Why would order of call cause a problem? – Majed DH Oct 23 '18 at 17:15
25

As suggested above, separate statements will definitely work. But the trick here is to setCustomAnimation before setting transaction type viz.add, replace, etc. else it doesn't. So, applying the same logic, method chaining also works. eg.

getSupportFragmentManager()
        .beginTransaction()
        .setCustomAnimations(R.anim.a_slide_up,
                             R.anim.a_slide_down,
                             R.anim.a_slide_up,
                             R.anim.a_slide_down)
        .add(R.id.root_layout, 
             MyFrag.newInstance())
        .addToBackStack("MyFrag")
        .commit();

Putting it here, so that someone who prefers method chaining finds it helpful. Cheers!

Harisewak
  • 570
  • 6
  • 15
7

Leaving this here as it's the most popular question. I had the same problem with fragment transaction not animating. The culprit was having the attribute android:animateLayoutChanges set to true in the containing layout.

I hope it helps someone save some time looking for a solution as it can be hard to notice when having nested layouts in different files.

  • Animation working now but new problem- it is changed animation behavior. before it is sliding horizontally (as expected). now expanding from corner – Sharad Kale Oct 23 '20 at 13:39
0

Another reason can be unnecessarily placing fragmentTransaction.show() before commit. This makes pop transitions not show on some android API versions.

Carson Holzheimer
  • 2,890
  • 25
  • 36
0

for me the problem was using Fragment.getChildFragmentManager() I switched to Fragment.getParentFragmentManager() and animations suddenly started working.

Ofek Regev
  • 467
  • 4
  • 18