简体   繁体   English

如何使用导航架构组件避免片段重新创建?

[英]How to avoid fragment recreation using navigation architecture component?

I have 4 fragments in a drawer, lets call them fragment A, B, C, D .我在一个抽屉里有 4 个片段,我们称它们为片段A, B, C, D I use navigation architecture to bind my fragments with the drawer.我使用导航架构将我的片段与抽屉绑定。

Fragment A is my entry point and from there I can navigate anywhere on the drawer. Fragment A 是我的入口点,从那里我可以在抽屉上的任何地方导航。

Suppose I take the path A > B > C > B > C假设我走路径A > B > C > B > C

If I press back from C, it goes to B then to C again and then to B and finally to A, but I don't want that.如果我从 C 向后按,它会转到 B,然后再转到 C,然后转到 B,最后转到 A,但我不想要那样。

When I press the back button from C, I want it to just go back to B then A without recreating fragments that are already in the back stack.当我从 C 按下后退按钮时,我希望它回到 B 然后 A 而不重新创建已经在后退堆栈中的片段。 Can someone please help on how to achieve this ?有人可以帮助如何实现这一目标吗?

When I press the back button from C, I want it to just go back to B then A without recreating fragments that are already in the back stack.当我从 C 按下后退按钮时,我希望它回到 B 然后 A 而不重新创建已经在后退堆栈中的片段。

When you are navigating to a destination, you can pop the old instance of the destination from the back stack.当您导航到目的地时,您可以从返回堆栈中弹出目的地的旧实例。 Just add the pop inclusive to all your action in nav graph.只需在导航图中将流行元素添加到您的所有action中即可。

<!-- Add this in the actions navigating to B -->
app:popUpTo="@id/B" 
app:popUpToInclusive="true"

Explanation解释

What the above pop behavior will do is, when you are navigating from, say C > B , it will pop everything till B (inclusive) from the back stack and add the latest instance of B on the back stack.上面的弹出行为会做的是,当您从C > B导航时,它将从返回堆栈中弹出所有内容直到 B(包括),并将 B 的最新实例添加到返回堆栈中。

A > B > C > B > C will have a back stack: A > B > C > B > C将有一个返回堆栈:

A
A, B
A, B, C
A, B
A, B, C

Note: The inherent assumption here is that this is not a valid case in your example.注意:这里的固有假设是这在您的示例中不是有效的情况。 A > B > C > D > B > C leads to a back tracking of C > B > D > A . A > B > C > D > B > C导致C > B > D > A的回溯。 Please be aware that in the above solution when you navigate to second B, it will pop out D from you back stack too!请注意,在上述解决方案中,当您导航到第二个 B 时,它也会从您的后堆栈中弹出 D!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android导航架构组件避免了Fragment娱乐 - Android Navigation Architecture component avoid Fragment recreation 当使用导航架构操作点击返回按钮时,如何避免片段重新生成? - How to avoid fragment recreation when tap back button using navigation architecture actions? 如何避免片段娱乐? - How to avoid Fragment recreation? 如果使用导航架构组件已经存在,如何避免将片段添加到后台堆栈? - How to avoid adding a fragment to the backstack if it already exists using Navigation Architecture Component? 避免使用导航架构组件弹回特定片段 - Avoid to popback to specific fragment with Navigation Architecture Component 导航架构组件 - BottomNavigationView 避免片段重新加载 - Navigation Architecture Component - BottomNavigationView avoid fragment reloading 使用导航组件重新创建片段 - Recreation of Fragments Using Navigation Component 如何使用导航架构组件从片段中获取结果? - How to get a result from fragment using Navigation Architecture Component? 使用导航架构组件时如何与其他片段交互? - How to interact with other fragment when using Navigation architecture component? 如何导航到Half Fragment? (导航架构组件) - How to navigate to Half Fragment ? (Navigation Architecture Component)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM