Exit animation not working; FragmentTransaction custom animation does not work for hide

Leo K picture Leo K · Dec 10, 2012 · Viewed 11.2k times · Source

I use show/hide to display a fragment that takes up part of the screen. For some reason when the fragment is shown the slide_in_left animation plays, but when the fragment is being hidden there is no animation, the fragment just disappears. I've tried using the slide_in_left animation for both exit and enter, this did not help. When a trace the code into the support package, the animation does get created and the code for displaying it is being executed. (I traced the .hide call)

FragmentManager fm = _activity.getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.my_fragment);
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left);
if (fragment.isHidden()) {
    ft.show(fragment);
    fragment.setUserVisibleHint(true);
} else {
    ft.hide(fragment);
}
ft.commit();

Just in case here's the xml for the slide_out_left animation

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-50%p"
        android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

Edit: It's possible that the problem has something to do with the fact that my_fragment shares screen width with another fragment cotaining a webview. When .show is executed for my_fragment it becomes visible and shares space within a horizontal linear layout (how much screen width each of the two fragments takes up is determined by the weight parameter).

Answer

Amlan picture Amlan · Sep 26, 2014

Try to use setCustomAnimations(int enter, int exit, int popEnter, int popExit) instead of setCustomAnimations(int enter, int exit) When you will call popupbackstack exit animation wont be called