support-v4:27.1.0 fragment custom animations do not work as expected
Asked Answered
O

3

8

Fragment animations do not work properly with support-v4:27.1.0

getSupportFragmentManager()
       .beginTransaction()
       .setCustomAnimations(ENTER_ANIM , LEAVE_ANIM)
       .replace(R.id.main_activity_fragment_place_holder, fragment)
       .addToBackStack(tag)
       .commitAllowingStateLoss();

enter animation

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />

leave animation

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="500" />
Oglesby answered 8/3, 2018 at 18:43 Comment(1)
enter and exit animation that set with setCustomAnimations()Oglesby
W
17

I just hit the same problem. Support library 27.1.0 seems to have a problem with anim transitions that use the alpha property.

My impression is that the transition engine does not correctly implement a "fill-after", so that the fragment alpha quickly bounces back to 1 before the fragment is removed. This causes a blinking effect where the replaced fragment is briefly visible and then disappears.

I resolved the issue switching to animator transitions.

I.e. replaced my /res/anim/fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0"
    android:toAlpha="1"
    android:duration="500"
    />

with an analogous /res/animator/fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="alpha"
    android:valueFrom="0"
    android:valueTo="1"
    android:duration="500"
    />

I did the same for the fade_out transition.

Wrung answered 16/3, 2018 at 8:50 Comment(0)
M
5

The blinking effect was fixed in the latest support library version 27.1.1. (see issue 74051124)

Magically answered 6/4, 2018 at 20:0 Comment(1)
Should be the accepted answer. I tested. Bug does not exist on support v27.1.1Spinoza
U
3

I have the exact same problem after upgrading the support library from 27.0.2 to 27.1.0. Instead of fading smoothly, the fragments blink several times.

It seems that all animators work as expected, except alpha animators.

However, I have found a workaround for this bug: If you disable the enter animation, the transition still fades. It does not fade in the exact same way as before, but it looks good (or even better) in my opinion.

The new enter animation (which I have named nothing.xml) is:

<?xml version="1.0" encoding="utf-8"?>
<set/>
Unwished answered 13/3, 2018 at 9:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.