Leo K Leo K - 4 days ago 5
Android Question

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

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

Personally for me this is a bug with the zPosition for Fragment animations.

What you see is the new fragment "appearing" this is because the new fragment is attached underneath the current one.

So when trying to animate a fragment 'Over the top' of an existing fragment, it appears like nothing is happening then the new one just 'appears'.

I have tried many work arounds, playing with zPosition (only affects windows not layouts, so no effect to fragments), onAnimationCreate() bringing the root layout to front or even on animation start and stop... seems to do nothing.

So at this point in time without writing a complete custom animation for fragment transitions, they are a bit buggy at the moment.

You may have to play with. .add(Fragment) wait for it to be added, then call .remove(Fragment) removing the old fragment, that way the new fragment is physically placed on top of the existing fragment.

Comments