弹出输入动画不适用于 Jetpack 导航组件

Pop enter animation not working with the Jetpack Navigation Component

我一直在使用 Jetpack Navigation 组件(2.3.5,虽然我也测试过旧版本)并尝试在两个简单的目的地之间添加转换。但是,使用 popEnterAnim 属性指定的转换根本不会 运行。相反,重新进入的片段会立即出现。

我已经成功地用一个非常简单的例子重新创建了它。片段布局只是一个 TextView.

主要activity

<androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph" />
class ActivityMain : AppCompatActivity(R.layout.main)

片段 1

class Fragment1 : Fragment(R.layout.fragment_1) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

        view.setOnClickListener {

            findNavController().navigate(R.id.action_fragment1_to_fragment2)
        }
    }
}

片段 2

class Fragment2: Fragment(R.layout.fragment_2)

导航图

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/nav_graph.xml"
    app:startDestination="@id/fragment1">

    <fragment
        android:id="@+id/fragment1"
        android:name="uk.henrytwist.projectsource.Fragment1"
        android:label="MainFragment" >
        <action
            android:id="@+id/action_fragment1_to_fragment2"
            app:destination="@id/fragment2"
            app:enterAnim="@anim/transition_rise_up_enter"
            app:exitAnim="@anim/transition_fade_exit"
            app:popEnterAnim="@anim/transition_fade_pop_enter"
            app:popExitAnim="@anim/transition_rise_up_pop_exit" />
    </fragment>
    <fragment
        android:id="@+id/fragment2"
        android:name="uk.henrytwist.projectsource.Fragment2"
        android:label="Fragment2" />
</navigation>

原来这实际上是由于AndroidX片段库(androidx.fragment:fragment:1.3.2)中的一个错误,现在已经在版本1.3.3中得到纠正。 changelog 确认修复:

Fixed a regression introduced in Fragment 1.3.2 which would cause popEnter animations to not run when popping a FragmentTransaction that included a setPrimaryNavFragment operation, such as those used by NavHostFragment. (I38c87, b/183877426)