1

I use navigation component in my app. I set slide up/down animation for fragment transaction.But I didn't get desired result.The behavior I want is: when new fragment(A) replace to previous fragment(B),previous fragment keep be fixed(without animation) and new fragment come down from up to up and when user press back button,fragment B slide from up to down and fragment A keep fixed. The behavior that I get at the moment: when new fragment(A) replace to previous fragment(B),previous fragment also moved and when user press back button,fragment A also moved. this is Four animation file i have used: enter_from_up.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:interpolator="@android:anim/linear_interpolator">

<translate
        android:fromXDelta="0%" android:toXDelta="0%"
        android:fromYDelta="-100%" android:toYDelta="0%"
        android:duration="350" />

exit_from_down.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:interpolator="@android:anim/linear_interpolator">

<translate
        android:fromXDelta="0%" android:toXDelta="0%"
        android:fromYDelta="0%" android:toYDelta="100%"
        android:duration="350" />

enter_from_down.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:interpolator="@android:anim/linear_interpolator">

<translate
        android:fromXDelta="0%" android:toXDelta="0%"
        android:fromYDelta="100%" android:toYDelta="0%"
        android:duration="350" />

exit_from_up.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false"
    android:interpolator="@android:anim/linear_interpolator">

    <translate
            android:fromXDelta="0%" android:toXDelta="0%"
            android:fromYDelta="0%" android:toYDelta="-100%"
            android:duration="350" />
</set>

And this is code of my navigation graph:

 <action 
android:id="@+id/action_welcomeFragment_to_signUpOneFragment" app:destination="@id/signUpOneFragment"
app:enterAnim="@anim/enter_from_down" 
app:exitAnim="@anim/exit_from_up"
app:popEnterAnim="@anim/enter_from_up" 
app:popExitAnim="@anim/exit_from_down"/>
maryam
  • 1,437
  • 2
  • 18
  • 40

1 Answers1

6

I had a similar issue, here is the solution I found: an anim ("slide_up") that represent the enter animation of the foreground fragment, an anim ("slide_down") that represent the exit animation of the foreground fragment and an anim ("stationary") that represent the animation for both exit and enter animation of the background fragment.

slide_up.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromYDelta="100%"
    android:toYDelta="0%" />

slide_down.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromYDelta="0%"
    android:toYDelta="100%" />

stationary.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime" />

nav_graph.xml (partial):

<action
    android:id="@+id/action_from_backgroundFragment_to_ForegroundFragment"
    app:destination="@id/foregroundFragment"
    app:enterAnim="@anim/slide_up"
    app:exitAnim="@anim/stationary"
    app:popEnterAnim="@anim/stationary"
    app:popExitAnim="@anim/slide_down" />

Maybe this can help you !