You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

跨模块导航至另一导航图指定Fragment崩溃问题求助

跨模块嵌套导航图跳转崩溃问题解决

问题背景

模块1的first_nav_graph中,CardMenuFragment跳转至模块2second_nav_graph的起始Fragment可正常工作,但直接跳转至该嵌套图内的increaseLimitFragment或increaseLimitStatusFragment时,调用findNavController().navigate()会触发崩溃,异常信息如下:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ru.otpbank.mobile, PID: 11072
    java.lang.IllegalArgumentException: Navigation destination ru.otpbank.mobile:id/increaseLimitFragment referenced from action ru.otpbank.mobile:id/actionCardMenuFragmentToIncreaseLimitFragment cannot be found from the current destination Destination(ru.otpbank.mobile:id/card_menu_fragment) label=CardMenuFragment

相关配置代码:

first_nav_graph.xml(片段)

<include app:graph="@navigation/second_nav_graph" />
...

<dialog
    android:id="@+id/card_menu_fragment"
    android:name="ru.bank.mobile.presentation.ui.product_menus.CardMenuFragment"
    android:label="CardMenuFragment"
    tools:layout="@layout/fragment_menu">
    <!-- 正常工作的跳转:指向整个嵌套导航图 -->
    <action
        android:id="@+id/actionCardMenuFragmentToSecondNavGraph"
        app:destination="@id/second_nav_graph"/>

    <!-- 崩溃的跳转:直接指向嵌套图内的Fragment -->
    <action
        android:id="@+id/actionCardMenuFragmentToIncreaseLimitFragment"
        app:destination="@id/increaseLimitFragment" />

    <action
        android:id="@+id/actionCardMenuFragmentToIncreaseLimitStatusFragment"
        app:destination="@id/increaseLimitStatusFragment" />
</dialog>

second_nav_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/second_nav_graph"
    app:startDestination="@id/increaseLimitFragment">

    <fragment
        android:id="@+id/increaseLimitFragment"
        android:name="com.example.credit_limit_increase.limit_increase_application.IncreaseLimitFragment"
        android:label="IncreaseLimitFragment" />

    <fragment
        android:id="@+id/increaseLimitStatusFragment"
        android:name="com.example.credit_limit_increase.limit_increase_status.IncreaseLimitStatusFragment"
        android:label="fragment_increase_limit_status"
/>
</navigation>

问题原因

Android Navigation组件中,通过<include>引入的嵌套导航图是独立的导航单元,其内部Fragment的ID仅在该嵌套图的导航范围内可见。父导航图(first_nav_graph)无法直接引用嵌套图内的Fragment ID,因为这些ID不属于父图的导航节点集合,因此会抛出“无法找到目标”的异常。

解决方法

方法1:跳转至嵌套图并通过参数指定目标(最简单)

保留指向整个嵌套图的action,跳转时传递参数,让嵌套图的起始Fragment根据参数导航到目标页面:

  1. 修改CardMenuFragment中的跳转代码,传递目标标识:
// 跳转到increaseLimitStatusFragment的示例
findNavController().navigate(
    R.id.actionCardMenuFragmentToSecondNavGraph,
    bundleOf("TARGET_FRAGMENT" to "STATUS")
)

// 跳转到increaseLimitFragment则传递"APPLICATION"
  1. 在increaseLimitFragment的onViewCreated中处理参数:
arguments?.getString("TARGET_FRAGMENT")?.let { target ->
    if (target == "STATUS") {
        findNavController().navigate(R.id.increaseLimitStatusFragment)
    }
}

方法2:使用深层链接直接跳转

为嵌套图内的Fragment配置深层链接,通过URI直接导航:

  1. 在second_nav_graph.xml中为目标Fragment添加深层链接:
<fragment
    android:id="@+id/increaseLimitFragment"
    android:name="com.example.credit_limit_increase.limit_increase_application.IncreaseLimitFragment"
    android:label="IncreaseLimitFragment">
    <deepLink app:uri="app://your-app/limit-increase" />
</fragment>

<fragment
    android:id="@+id/increaseLimitStatusFragment"
    android:name="com.example.credit_limit_increase.limit_increase_status.IncreaseLimitStatusFragment"
    android:label="fragment_increase_limit_status">
    <deepLink app:uri="app://your-app/limit-status" />
</fragment>
  1. 在CardMenuFragment中通过URI跳转:
// 跳转到increaseLimitStatusFragment
findNavController().navigate(Uri.parse("app://your-app/limit-status"))

// 跳转到increaseLimitFragment
findNavController().navigate(Uri.parse("app://your-app/limit-increase"))

方法3:将嵌套图的Fragment暴露为全局目标(不推荐)

如果不需要保持模块化,可以将second_nav_graph中的Fragment直接添加到first_nav_graph中,但这会导致导航图耦合度升高,不建议用于跨模块场景。


内容的提问来源于stack exchange,提问作者faritowich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 10:37:00