0

I'm trying to put slide animation on a FragmentContainerView. However, the fragment that should appear flickers before starting the animation.

To summarize, when the transaction begins, the current fragment slides out, the next fragment flickers and then slides in.

I'm not asking for direct solutions, but if somebody knows what can cause this kind of bug, because I don't even know what to look at to solve this problem.

Edit: Code samples

FragmentContainerView (XML)

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/frame_cigarette"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/toolbar_activity_filter"
        app:layout_constraintBottom_toBottomOf="parent"
        />

Code for fragment transition

supportFragmentManager.beginTransaction()
                        .setCustomAnimations(
                            R.anim.slide_in_right,
                            R.anim.slide_out_left,
                            R.anim.slide_in_left,
                            R.anim.slide_out_right
                        )
                        .replace(
                            R.id.frame_cigarette,
                            CigaretteCravingFragment.newInstance(),
                            CigaretteCravingFragment::class.java.simpleName
                        )
                        .addToBackStack(CigaretteCravingFragment::class.java.simpleName)
                        .commit()

Code for an exit slide animation (slide out right)


<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromXDelta="0"
        android:toXDelta="100%p" />
</set>

Code for an enter slide animation (slide in left)


<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromXDelta="-100%p"
        android:toXDelta="0" />
</set>

A.Danibo
  • 494
  • 1
  • 4
  • 16

1 Answers1

0

I had the same problem and I solved this few moments ago. In my case Im using navigation graph to define all animation between fragments but in your case should work the same because setCustomAnimations also can take @AnimatorRes. You have to use ObjectAnimator and custom layout. Inspiration from: Animate the transition between fragments

CustomConstraintLayout.java

public class CustomConstraintLayout extends ConstraintLayout {
    
    public float getXFraction() {
        return getX() / getWidth();
    }

    public float getYFraction() {
        return getY() / getHeight();
    }

    public void setXFraction(float xFraction) {
        final int width = getWidth();
        setX((width > 0) ? (xFraction * width) : -9999);
    }

    public void setYFraction(float yFraction) {
        final int height = getHeight();
        setY((height > 0) ? (yFraction * height) : -9999);
    }
}

slide_in_from_bottom_animator.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:valueType="floatType"
        android:propertyName="yFraction"
        android:valueFrom="1.0"
        android:valueTo="0.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:duration="@integer/change_view_anim_time"/>
</set>

YourFragment.xml

<?xml version="1.0" encoding="utf-8"?>
<com.yourapp.ui.CustomConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <!-- some stuff -->

</com.yourapp.ui.CustomConstraintLayout>