2 years ago

#35562

test-img

Tommy Jackson

Dynamic fragment animations with Navigation Component

I have 3 fragments - let's call them A, B, and C - all defined within a navigation graph. The exact scenario I'm working on is this:

  • A starts B with a slide up animation
  • B goes to C with a slide over animation
  • Pressing the back button from C will reverse the slide over animation back to B, and then pressing back from B will slide down back to A.
  • B and C both have "X" buttons that pop back to A
  • When the "X" button is clicked, the closing fragment should slide down to reveal A (reverse of the original slide up that started B)

The problem is C already has its animations defined to be the slide over ones like this:

    <action
        android:id="@+id/to_fragment_c"
        app:destination="@id/fragment_c"
        app:enterAnim="@anim/horizontal_slide_enter"
        app:exitAnim="@anim/horizontal_slide_exit"
        app:popEnterAnim="@anim/horizontal_slide_pop_enter"
        app:popExitAnim="@anim/horizontal_slide_pop_exit"/>

While B is defined like this:

    <action
        android:id="@+id/to_fragment_b"
        app:destination="@id/fragment_b"
        app:enterAnim="@anim/vertical_slide_enter"
        app:exitAnim="@anim/vertical_slide_exit"
        app:popEnterAnim="@anim/vertical_slide_pop_enter"
        app:popExitAnim="@anim/vertical_slide_pop_exit" />

A is the start destination of the graph so I'm going from C to A like this:

        navController?.graph?.startDestinationId?.let {
            navController?.popBackStack(it, false)
        }

How can I do the slide over animation when going from C back to B, but then do the slide down animation when going from C back to A?

android

android-architecture-navigation

android-transitions

0 Answers

Your Answer

Accepted video resources