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

如何从一个Navigation Graph的Fragment跳转至另一个Navigation Graph的Fragment?

实现跨Activity导航图的Fragment跳转

先修正现有代码错误

你的activity_why.xml中,NavHostFragment引用的导航图是nav_graph,这会导致WhyActivity加载HelloFragment而非WhyFragment,需改为对应的nav1:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WhyActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav1" /> <!-- 此处修改为nav1 -->

</androidx.constraintlayout.widget.ConstraintLayout>

实现HelloFragment到WhyFragment的跳转

由于两个Fragment分属不同Activity的导航图,提供两种常用实现方式:

方式一:直接启动WhyActivity

在HelloFragment中添加跳转逻辑(比如给TextView绑定点击事件),直接启动WhyActivity即可,因为WhyActivity默认加载的就是WhyFragment。

修改HelloFragment.kt:

package com.example.multiplenavgraph

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

class HelloFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_hello, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 给Hello文本添加点击跳转逻辑
        view.findViewById<View>(android.R.id.text1).setOnClickListener {
            val intent = Intent(requireContext(), WhyActivity::class.java)
            startActivity(intent)
        }
    }
}

方式二:使用Navigation Deep Link(推荐)

该方式更贴合Navigation组件的设计思路,通过Deep Link实现跨导航图跳转:

  1. 给nav1.xml中的WhyFragment添加Deep Link配置:
<?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/nav1"
    app:startDestination="@id/whyFragment">

    <fragment
        android:id="@+id/whyFragment"
        android:name="com.example.multiplenavgraph.WhyFragment"
        android:label="fragment_why"
        tools:layout="@layout/fragment_why">
        <!-- 添加Deep Link配置 -->
        <deepLink app:uri="app://multiplenavgraph/why" />
    </fragment>
</navigation>
  1. 在AndroidManifest.xml中给WhyActivity添加Deep Link的intent-filter:
<activity
    android:name=".WhyActivity"
    android:exported="false">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- 匹配Deep Link的URI -->
        <data android:scheme="app" android:host="multiplenavgraph" android:path="/why" />
    </intent-filter>
</activity>
  1. 在HelloFragment中通过NavController导航到该Deep Link:
package com.example.multiplenavgraph

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import android.net.Uri

class HelloFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_hello, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        view.findViewById<View>(android.R.id.text1).setOnClickListener {
            val deepLinkUri = Uri.parse("app://multiplenavgraph/why")
            findNavController().navigate(deepLinkUri)
        }
    }
}

说明

两种方式均可实现跳转:方式一简单直接,适合基础场景;方式二更符合Navigation组件的架构设计,便于统一管理跳转逻辑,也支持后续扩展更多Deep Link场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 03:25:53