简体   繁体   English

AndroidX 导航 navigateUp 两次

[英]AndroidX navigation navigateUp twice

I am using the new AndroidX navigation framework.我正在使用新的 AndroidX 导航框架。

I have a few fragments all linked in a navigation chain.我有几个片段都链接在导航链中。

FragmentA --> FragmentB --> FragmentC片段 A --> 片段 B --> 片段 C

FragmentC has a Cancel button that should send me up all the way back to FragmentA. FragmentC 有一个 Cancel 按钮,它应该让我回到 FragmentA。

Should I do the following:我应该执行以下操作:
on FragmentC call the method:在 FragmentC 上调用方法:

 Navigation.findNavController(view).navigateUp();

then on FragmentB listen to some callback and using some passed parameter or argument trigger another navigateUp() function from FragmentB然后在 FragmentB 上听一些回调并使用一些传递的参数或参数从 FragmentB 触发另一个 navigateUp() 函数

or is there some method that will do the equivalent of navigateUpTwice()或者是否有一些方法可以做相当于 navigateUpTwice()

我最终做的是

Navigation.findNavController(view).popBackStack(R.id.fragmant_a,false)

In your navigation.xml file under the action that you have created to navigate to starting fragment (FragmentA in your case) add the following在您创建的用于导航到起始片段(在您的情况下为 FragmentA)的操作下的 navigation.xml 文件中,添加以下内容

<action
....
app:popUpTo="@id/fragmentA"
app:popUpToInclusive="false"/>

This will pop up all the fragments until FragmentA and will exclude FragmentA since popUpToInclusive is set to false .这将弹出所有片段,直到 FragmentA 并将排除 FragmentA,因为popUpToInclusive设置为false

Edit: The full form of your action under FragmentC tag of your navigation.xml file will be something similar to this:编辑:您在 navigation.xml 文件的 FragmentC 标签下的完整操作形式类似于:

<fragment
    android:id="@+id/FragmentC"
    android:name="com.yourdomain.FragmentC"
    android:label="FragmentC">
    <action
        android:id="@+id/action_fragmentC_to_fragmentA"
        app:destination="@id/fragmentA"
        app:popUpTo="@id/fragmentA"
        app:popUpToInclusive="false"/>
</fragment>

您可以从 FragmentB --> FragmentC 在您的操作中将 pop 设置为 FragmentA,然后当您按回时它会转到 Fragment A 而不是 Fragment B

Try with this, it works for me.试试这个,它对我有用。

findNavController().navigateUp()
findNavController().navigateUp()

So I found myself in a situation where I had to navigateUp() twice, but with a twist: I could reach the view through several paths.所以我发现自己处于必须navigateUp()两次的情况,但有一个转折点:我可以通过多条路径到达视图。 Think of it like this:可以这样想:

Fragment A --> Fragment C --> Fragment D片段 A --> 片段 C --> 片段 D

Fragment B --> Fragment C --> Fragment D片段 B --> 片段 C --> 片段 D

In a normal situation, using the popTo xml attribute or the popBackStack() method would work.在正常情况下,使用popTo xml 属性或popBackStack()方法都可以。 However it is unusable here.但是在这里是不能用的。 Fortunately, what you can do is:幸运的是,您可以做的是:

val navController = NavHostFragment.findNavController(this)
navController.navigateUp()
navController.navigateUp

As other people have pointed out, this is absolutely not optimised performance-wise.正如其他人指出的那样,这绝对没有在性能方面进行优化。 In any situation where you can reach Fragment D through a single path, use popTo instead.在任何可以通过单一路径到达片段 D 的情况下,请改用popTo

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM