Search code examples
androidandroid-architecture-navigation

Navigation Architecture Component: transition animations not working for dialog


I have a <dialog in my nav graph with enter/exit anims but the animations aren't working for the dialog. I've tested them on <fragment nodes and those work fine.

For clarification, the dialog being referenced is a DialogFragment

Is this a limitation or am I doing something wrong?

Here's the relevant snippet from my nav graph:

<fragment
        android:id="@+id/fragment_home"
        android:name="com.my.project.fragments.HomeFragment"
        android:label="@string/nav_home"
        tools:layout="@layout/fragment_home">
        <action
            android:id="@+id/action_fragment_home_to_fragment_dialog_new_user_welcome"
            app:destination="@id/fragment_dialog_new_user_welcome"
            app:enterAnim="@anim/nav_fade_enter_anim"
            app:exitAnim="@anim/nav_fade_exit_anim"
            app:popUpTo="@layout/fragment_home" />
    </fragment>

    <dialog
        android:id="@+id/fragment_dialog_new_user_welcome"
        android:name="com.my.project.fragments.NewUserWelcomeDialog"
        tools:layout="@layout/fragment_dialog_new_user_welcome">

        <action
            android:id="@+id/action_fragment_dialog_new_user_welcome_to_activity_discover_detail"
            app:destination="@id/fragment_discover_detail"
            app:launchSingleTop="true"
            app:popUpTo="@id/fragment_home" />
    </dialog>

Here's the enter anim:

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

Here's the exit anim:

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

Solution

  • As of version 2.2.0-alpha02 this is the limitation of Navigation component. You can view the source code of DialogFragmentNavigator

    However, you can pretty easily achieve animation for DialogFragment using the following steps:

    1. Create a style mentioning the enter and exit animations from anim folder:
        <style name="MyDialogAnimation">
            <item name="android:windowEnterAnimation">@anim/enter_anim</item>
            <item name="android:windowExitAnimation">@anim/exit_anim</item>
        </style>
    
    1. Inside the DialogFragment set the style as windowAnimations
         override fun onActivityCreated(savedInstanceState: Bundle?) {
            super.onActivityCreated(savedInstanceState)
            dialog?.window?.attributes?.windowAnimations = R.style.MyDialogAnimation
        }
    

    Find more here.