Android 导航组件弹出到转换问题 [英] Android Navigation Component pop to transition issue
问题描述
我有两个动作
动作1
<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" />
动作2
<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 导航组件弹出到转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!