Smoothing performance on Fragment transitions

Suppose you're doing a pretty standard Fragment replacement with a custom animation:

getSupportFragmentManager()
    .beginTransaction()
    .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
    .replace(android.R.id.content, new MyFragment())
    .commit();

You may notice that the performance can be a bit rough, not as smooth as you'd like. A common way to improve Android animation performance is to use hardware layers. Normally you'd add it to the animation directly but with fragments you don't get access to it unless you take advantage of Fragment.onCreateAnimation()*. Here's how it looks:

public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
  Animation animation = super.onCreateAnimation(transit, enter, nextAnim);

  // HW layer support only exists on API 11+
  if (Build.VERSION.SDK_INT >= 11) {
    if (animation == null && nextAnim != 0) {
      animation = AnimationUtils.loadAnimation(getActivity(), nextAnim);
    }

    if (animation != null) {
      getView().setLayerType(View.LAYER_TYPE_HARDWARE, null);

      animation.setAnimationListener(new AnimationListener() {
        public void onAnimationEnd(Animation animation) {
          getView().setLayerType(View.LAYER_TYPE_NONE, null);
        }

        // ...other AnimationListener methods go here...
      });
    }
  }

  return animation;
}

Now the animation should be a lot more smooth! In my own code, I've overridden this method in a base Fragment from which all others extend so that I always get this feature (though if you're more particular you could only apply it to certain Fragments).

* If you're not using the support library, then you'll be overriding Fragment.onCreateAnimator() and using animator-based classes.