欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > Jetpack Navigation 实战:Fragment 和 Activity 的交互与导航

Jetpack Navigation 实战:Fragment 和 Activity 的交互与导航

2025/3/13 11:07:54 来源:https://blog.csdn.net/tangweiguo03051987/article/details/146193367  浏览:    关键词:Jetpack Navigation 实战:Fragment 和 Activity 的交互与导航

在 Android 开发中,使用 Jetpack Navigation 组件可以方便地管理 Fragment 和 Activity 之间的导航。以下是如何使用 Jetpack Navigation 实现 Fragment 之间、Activity 之间以及 Activity 与 Fragment 之间跳转的实战示例。
1. 添加依赖
首先,在 build.gradle 文件中添加 Navigation 组件的依赖:

dependencies {implementation "androidx.navigation:navigation-fragment-ktx:2.5.3"implementation "androidx.navigation:navigation-ui-ktx:2.5.3"
}

2. 创建导航图
res/navigation 目录下创建一个导航图文件(例如 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"app:startDestination="@id/fragmentA"><!-- Fragment 之间的导航 --><fragmentandroid:id="@+id/fragmentA"android:name="com.example.FragmentA"tools:layout="@layout/fragment_a"><actionandroid:id="@+id/action_fragmentA_to_fragmentB"app:destination="@id/fragmentB" /></fragment><fragmentandroid:id="@+id/fragmentB"android:name="com.example.FragmentB"tools:layout="@layout/fragment_b"><actionandroid:id="@+id/action_fragmentB_to_fragmentA"app:destination="@id/fragmentA" /></fragment><!-- Activity 之间的导航 --><activityandroid:id="@+id/activityB"android:name="com.example.ActivityB"tools:layout="@layout/activity_b" /><!-- Activity 与 Fragment 之间的导航 --><fragmentandroid:id="@+id/fragmentC"android:name="com.example.FragmentC"tools:layout="@layout/fragment_c"><actionandroid:id="@+id/action_fragmentC_to_activityB"app:destination="@id/activityB" /></fragment></navigation>

3. 在 Activity 中设置 NavController
在你的主 Activity 中,设置 NavController 并将其与 NavHostFragment 绑定:

class MainActivity : AppCompatActivity() {private lateinit var navController: NavControlleroverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragmentnavController = navHostFragment.navController// 设置 NavigationUI 与 BottomNavigationView 或 NavigationView 的绑定(可选)setupNavigationUI()}private fun setupNavigationUI() {// 如果你有 BottomNavigationView 或 NavigationView,可以在这里绑定val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav_view)bottomNavigationView?.setupWithNavController(navController)}
}

4. Fragment 之间的跳转
FragmentA 中,使用 NavController 跳转到 FragmentB

class FragmentA : Fragment() {override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {val view = inflater.inflate(R.layout.fragment_a, container, false)view.findViewById<Button>(R.id.button).setOnClickListener {findNavController().navigate(R.id.action_fragmentA_to_fragmentB)}return view}
}

5. Activity 之间的跳转
FragmentC 中,使用 NavController 跳转到 ActivityB

class FragmentC : Fragment() {override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {val view = inflater.inflate(R.layout.fragment_c, container, false)view.findViewById<Button>(R.id.button).setOnClickListener {findNavController().navigate(R.id.action_fragmentC_to_activityB)}return view}
}

在 ActivityB 中,你可以通过 Intent 启动其他 Activity 或返回上一个 Activity。

6. Activity 与 Fragment 之间的跳转
如果你想从 ActivityB 跳转回 FragmentC,可以在 ActivityB 中使用 NavController

class ActivityB : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_b)findViewById<Button>(R.id.button).setOnClickListener {val navController = findNavController(R.id.nav_host_fragment)navController.navigate(R.id.action_activityB_to_fragmentC)}}
}

7. 处理返回栈
在导航图中,你可以使用 popUpTopopUpToInclusive 属性来控制返回栈的行为。例如:

<actionandroid:id="@+id/action_fragmentA_to_fragmentB"app:destination="@id/fragmentB"app:popUpTo="@id/fragmentA"app:popUpToInclusive="true" />

这将在跳转到 FragmentB 时清除返回栈中的所有 FragmentA 实例。

8. 处理 Deep Links
你还可以使用 Navigation 组件处理 Deep Links。在导航图中为 Fragment 或 Activity 添加 deepLink

<fragmentandroid:id="@+id/fragmentC"android:name="com.example.FragmentC"tools:layout="@layout/fragment_c"><deepLink app:uri="example.com/fragmentC" />
</fragment>

然后在 AndroidManifest.xml 中为 Activity 添加 nav-graph

<activity android:name=".MainActivity"><nav-graph android:value="@navigation/nav_graph" />
</activity>

9.在导航图中定义参数
在 Jetpack Navigation 中,传递参数非常简单。我们可以在导航图中定义参数,并在跳转时通过 Bundle 传递数据。以下是一个简单的实战示例,展示如何在 Fragment 之间传递参数。

在导航图中定义参数
在 nav_graph.xml 中,为目标 Fragment 定义参数。例如,为 FragmentB 定义一个 String 类型的参数 userName:

<fragmentandroid:id="@+id/fragmentB"android:name="com.example.FragmentB"tools:layout="@layout/fragment_b"><!-- 定义参数 --><argumentandroid:name="userName"app:argType="string" /><actionandroid:id="@+id/action_fragmentA_to_fragmentB"app:destination="@id/fragmentB" />
</fragment>

在 FragmentA 中传递参数
在 FragmentA 中,使用 NavController 跳转到 FragmentB 并传递参数:

class FragmentA : Fragment() {override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {val view = inflater.inflate(R.layout.fragment_a, container, false)view.findViewById<Button>(R.id.button).setOnClickListener {// 创建 Bundle 并传递参数val bundle = Bundle()bundle.putString("userName", "JohnDoe")// 跳转到 FragmentB 并传递参数findNavController().navigate(R.id.action_fragmentA_to_fragmentB, bundle)}return view}
}

在 FragmentB 中接收参数
在 FragmentB 中,通过 arguments 获取传递过来的参数:

class FragmentB : Fragment() {override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {val view = inflater.inflate(R.layout.fragment_b, container, false)// 获取传递的参数val userName = arguments?.getString("userName")if (userName != null) {// 使用参数(例如显示在 TextView 中)view.findViewById<TextView>(R.id.textView).text = "Hello, $userName!"}return view}
}

使用 Safe Args 插件(推荐)
为了更安全地传递参数,可以使用 Navigation 的 Safe Args 插件。它会在编译时生成类型安全的代码,避免手动处理 Bundle。

添加 Safe Args 依赖
在 build.gradle 文件中添加 Safe Args 插件:

plugins {id "androidx.navigation.safeargs.kotlin"
}

在导航图中定义参数(同上)
在 FragmentA 中使用 Safe Args 传递参数

view.findViewById<Button>(R.id.button).setOnClickListener {// 使用 Safe Args 传递参数val action = FragmentADirections.actionFragmentAToFragmentB("JohnDoe")findNavController().navigate(action)
}

在 FragmentB 中使用 Safe Args 接收参数

class FragmentB : Fragment() {override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {val view = inflater.inflate(R.layout.fragment_b, container, false)// 使用 Safe Args 获取参数val args by navArgs<FragmentBArgs>()val userName = args.userName// 使用参数view.findViewById<TextView>(R.id.textView).text = "Hello, $userName!"return view}
}

总结
通过 Jetpack Navigation 组件,你可以轻松实现 Fragment 之间、Activity 之间以及 Activity 与 Fragment 之间的跳转。导航图提供了清晰的导航结构,并且可以方便地管理返回栈和 Deep Links。

通过以上步骤,你可以轻松地在 Fragment 之间传递参数:

普通方式:使用 Bundle 手动传递参数。

Safe Args:使用 Navigation 的 Safe Args 插件,实现类型安全的参数传递。

如果你需要传递复杂对象(如自定义类),可以结合 ParcelableSerializable 来实现。希望这个补充内容对你有帮助!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词