首页 > 解决方案 > 弹出输入动画不适用于 Jetpack 导航组件

问题描述

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

我设法用一个非常简单的例子重新创建了它。片段布局只是一个TextView.

主要活动

<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>

标签: androidandroid-fragmentsandroid-navigation-graph

解决方案


事实证明,这实际上是由于 AndroidX 片段库 ( androidx.fragment:fragment:1.3.2) 中的一个错误,该错误现已在版本中得到纠正1.3.3更改日志确认修复:

修复了 Fragment 1.3.2 中引入的回归,该回归会导致 popEnter 动画在弹出包含 setPrimaryNavFragment 操作的 FragmentTransaction 时无法运行,例如 NavHostFragment 使用的那些。(I38c87, b/183877426)


推荐阅读