Custom Fragment animations are different between the support library and vanilla

I've been converting a lot of my apps to the support library recently and ran into one issue worth mentioning. To be clear: I'm converting apps that use android.app.Fragment to now use support versions of Fragments.

If you do this and you're using FragmentTransaction.setCustomAnimations() you may be in for a shock. Your app will crash in the most glorious fashion:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unknown animation name: objectAnimator
 at android.view.animation.AnimationUtils.createAnimationFromXml(AnimationUtils.java:124)
 at android.view.animation.AnimationUtils.createAnimationFromXml(AnimationUtils.java:91)
 at android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:72)
 at android.support.v4.app.FragmentManagerImpl.loadAnimation(FragmentManager.java:710)
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:876)
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
 at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
 at android.os.Handler.handleCallback(Handler.java:587)
 at android.os.Handler.dispatchMessage(Handler.java:92)
 at android.os.Looper.loop(Looper.java:132)
 at android.app.ActivityThread.main(ActivityThread.java:4123)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:491)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
 at dalvik.system.NativeStart.main(Native Method)

The reason is that setCustomAnimations() in vanilla use object animators, whereas setCustomAnimations() in the support package use tween animations. It won't give you a compiler error because they're both referenced by resource id.

There's only one solution: convert your object animators to tween animations. The conversionwasn't difficult at all for me. In fact, it makes things a bit easier - object animators don't do percentages while tweens do, a problem I had struggled with before ("how do I get a Fragment to slide in from left to center?").

comments powered by Disqus