本文介绍了Android导航组件弹出过渡问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个动作

Action1

 <action
        android:id="@+id/actionBaseFragmentToAskForLocation"
        app:destination="@+id/introAskForLocationFragment"
        app:enterAnim="@anim/slide_in_right"
        app:exitAnim="@anim/slide_out_left"
        app:popEnterAnim="@anim/slide_in_left"
        app:popExitAnim="@anim/slide_out_right" />

Action2

<action
        android:id="@+id/actionIntroAskLocationToLogin"
        app:destination="@id/loginFragment"
        app:enterAnim="@anim/slide_in_right"
        app:exitAnim="@anim/slide_out_left"
        app:popEnterAnim="@anim/slide_in_right"
        app:popExitAnim="@anim/fade_out"
        app:popUpTo="@+id/app_main_navigation" />

我想要的是当第二个动作被触发时,我想清除后堆栈并仅将loginFragment设置为保留在堆栈中.

What i want is when the second action is triggered i want to clear the back stack and set only loginFragment to remain in the stack.

一个问题是,当我执行Action2时,'slide_out_right'作为退出动画执行

just one problem is when i perform the Action2, 'slide_out_right' is performed as exit animation

我知道,如果我们从堆栈中弹出片段,则将触发action1的"popExitAnim",而不是action2的"exitAnim".

I understand that if we pop the fragment from the stack the 'popExitAnim' of action1 will be triggered instead of 'exitAnim' of action2.

但是我想知道如何使该片段执行slide_out_left动画以退出并同时将其弹出堆栈.

but i want to know how can I make the fragment perform slide_out_left animation for exiting and also pop it out of the stack.

推荐答案

我最终在调用navigate的片段中重写了onCreateAnimation.此示例显示如何通过ID导航嵌套的导航图并有条件地替换 pop 退出动画(或popExitAnim).

I ended up overriding onCreateAnimation in the fragment that calls navigate. This exemple shows how to navigate through nested nav graphs by ID and replace the pop exit animation (or popExitAnim) conditionnally.

override fun onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int): Animation? {
    val navController = findNavController()
    val graph = navController.graph.findNode(R.id.onboardingGraph) as NavGraph
    val dest = graph.findNode(R.id.confirmationFragment)
    if (!enter && dest != null && navController.currentDestination?.id == dest.id) {
        return AnimationUtils.loadAnimation(requireContext(), R.anim.slide_out_left)
    }
    return super.onCreateAnimation(transit, enter, nextAnim)
}

请注意,这种特殊情况部分是由于幻灯片动画的方向性所致.

Note that this particular situation is partly due to the directional nature of slide animations.

这篇关于Android导航组件弹出过渡问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:16