30

I trying to use the new android compatibility package to include fragments into my project. I am trying to include a transition animation when I add a new fragment. The thing is only one of my animation work. The In animation works but the Out animation doesn't work. I read somewhere that it is a bug in the compatibility package. But I also read that the bug was fixed in the 3rd revision of the compatibility package. Can anyone help me with this issue

In Animation

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/anticipate_interpolator"
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="100%"        
android:toYDelta="0%"
android:duration="1000"/>

Out Animation

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
android:fromXDelta="0"
android:toXDelta="0"
android:zAdjustment="top"
android:fromYDelta="0%"        
android:toYDelta="100%"
android:duration="1000"/>

This is the code I use to add fragments

newFragment = new HelloWorldFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.bottom_top_animation, R.anim.top_bottom_animation);
ft.add(R.id.outer_layout, newFragment);
ft.addToBackStack(null);
ft.commit();
blessanm86
  • 31,439
  • 14
  • 68
  • 79
  • There is no info about resolving animation problem in 3rd revision: http://developer.android.com/sdk/compatibility-library.html#Notes – pawelzieba Sep 14 '11 at 12:56
  • I kinda abandoned fragments for the time being as I didnt feel it was mature enough to implment in a large project. – blessanm86 Sep 14 '11 at 13:39

4 Answers4

23

This works in the current version of the library, but it was definitely broken previously. You can use something like this:

final FragmentManager fm = getSupportFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_up, R.anim.slide_down, R.anim.slide_up, R.anim.slide_down)
  .add(R.id.fragment_container, new SomeFragment(), FRAGMENT_TAG)
  .addToBackStack(FRAGMENT_TAG)
  .commit();

where R.anim.slide_up is your in animation and R.anim.slide_down is your out animation. The second pair of params (3 and 4) for setCustomAnimations allow you to specify the pop in/out animations for popping the backstack (e.g., when the user presses back, the fragment will animate away with the animation specified as the fourth param).

Ian G. Clifton
  • 9,349
  • 2
  • 33
  • 34
  • 7
    it;s responding me Unknown animator name: translate... error...any idea if i am missing anything here? – CoDe Nov 27 '12 at 10:13
  • 6
    The slide_down animation does not seem to be working for me. Instead the fragment just gets removed and disappears with no slide animation. – Etienne Lawlor Sep 13 '13 at 01:13
  • You cannot use setCustomAnimations with get**Support**FragmentManager – MAGx2 Aug 05 '14 at 18:30
  • 1
    @MAGx2 You can use setCustomAnimations with getSupportFragmentManager(). However, after API 11, you must use ObjectAnimators, typically stored in R.animator, instead of translate animators. – Lo-Tan Oct 18 '14 at 20:30
  • 2
    Note that in the current version of the support library (21.0.2) , you can't use "setCustomAnimations". This is a known issue : http://code.google.com/p/android/issues/detail?id=77670 . – android developer Nov 30 '14 at 13:26
  • 16
    for 'com.android.support:support-v4:21.0.3': animations are working but it is important to call setCustomAnimations before add/replace. – stefan.nsk Jan 14 '15 at 05:37
  • For me the problem was that i was setting custom animations before adding the fragment. First set animations then add did the trick This post helped. Thanks – Mihaela Romanca Sep 14 '16 at 08:27
  • 1
    Make sure to call .setCustomAnimations() BEFORE calling .add() or .replace(). This had me banging me head for hours... – darkrider1287 Jan 12 '20 at 21:55
  • @stefan.nsk thank you for this...save me alot of time – DIRTY DAVE Nov 16 '20 at 08:19
8

I have found a workaround for this. Override onCreateAnimation(int transit, boolean enter, int nextAnim) in your fragment class then its working fine.

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    return enter ? AnimationUtils.loadAnimation(getActivity(), R.anim.grow_fade_in_center) : AnimationUtils.loadAnimation(getActivity(), R.anim.shrink_fade_out_center);
}
Mohit
  • 634
  • 5
  • 15
1

I am facing the same problem too, here's my situation:

  1. I want to do some UI change after exit animation. but I found that exit animation didn't work. my UI change codes are in onBackPressed().

my solution is as following:

  1. move UI change logic in onCreateAnimator(). and then exit animation works.
soolaugust
  • 245
  • 2
  • 10
0

Use the below example:

 private fun gotoFragment(fragment:Fragment){
     parentFragmentManager.commit {
        setCustomAnimations(
             R.anim.slide_in,
           R.anim.fade_out,
              R.anim.fade_in,
             R.anim.slide_out
        )
         setReorderingAllowed(true)
        replace(R.id.fragment_dashboard_container, fragment)
        addToBackStack(null)
    }
}

Here is the doc Fragment transactions

Shogun Nassar
  • 606
  • 7
  • 16