繁体   English   中英

Android Jetpack 导航:从“非导航”片段导航

[英]Android Jetpack Navigations: navigate from "non-navigation" fragment

我在我的应用程序中使用 Jetpack Navigation,我现在遇到了一个我不完全理解的情况。 我有一个片段CouponFragment ,它应该在多个地方使用(通过静态 xml 标记在其他片段内部和未连接到导航图的 bottomSHeetDialog 内部)。 我需要这个片段才能导航到一个WebviewFragment ,它是WebviewFragment中的一个目的地。 我为CouponFragment添加了导航条目:

在最简单的情况下( CouponFragment在另一个导航连接片段中),解决方案非常简单,我向包含CouponFragment的片段添加了一个动作,例如:

<fragment
        android:id="@+id/navigation_results"
        android:name="ResultsFragment"
        tools:layout="@layout/fragment_results">
        <action
            android:id="@+id/action_webview"
            app:destination="@id/navigation_webview" />
    </fragment>

这有效。 当我尝试从位于BottomSheetFragmentDialog内的CouponFragment导航时,真正的问题就出现了。 我尝试向调用对话框的导航连接片段添加一个操作:

<fragment
        android:id="@+id/navigation_profile"
        android:name="ProfileFragment"
        android:label="@string/title_profile"
        tools:layout="@layout/fragment_profile">
        <action
            android:id="@+id/action_webview"
            app:destination="@id/navigation_webview" />
    </fragment>

但这会导致此异常:

java.lang.IllegalArgumentException: Navigation action/destination package.name:id/action_webview cannot be found from the current destination Destination(package.name:id/navigation_webview) class=WebViewFragment

这很奇怪,因为看起来像是在目标片段上搜索动作。 我尝试将action_webview添加到navigation_webview

只是为了看看会发生什么,但没有任何作用。 不打印日志。

任何人都可以就如何解决这个问题给出提示吗?

我将开始不回答主要问题,只是因为细节非常模糊,并在必要时进行编辑。

要从 non_navigation 片段导航:

  1. 使用深层链接,这里是文档: https : //developer.android.com/guide/navigation/navigation-deep-link

  2. 只需使用 ParentFragmentDirections 和当前的 NavController。

例如:

  • FragmentA 与嵌套的 Non-Navigation-FragmentY
  • 片段B

从 FragmentY 导航到 FragmentB 只需从 MainNavigationHostFragment 加载 NavController 并设置为目标“FragmentADirections.actionFragmentA_to_FragmentB()”

暂无
暂无

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

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