两个Fragment如何实现类似Activity之间的跳转和传值?在AS3.2 beta版 发布时,出现了一个新的架构组件Navigation,可以实现Fragment之间的跳转以及Fragment跳转到Activity。
1.引入依赖:
implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha01'
implementation "android.arch.navigation:navigation-runtime:1.0.0-alpha01"2.创建Navigation配置文件
在res下创建navigation目录,新建Navigation resource file,这里就叫nav_graph.xml, 在设计视图下,可以添加fragment和Activity,然后通过连线的的方式添加之间的跳转关系,具体的触发事件,后面在代码中设置,这里只是设计了页面间的跳转关系
3.在需要使用Navigation的主Activity的布局文件中设置NavHostFragment, 参考代码如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent

本文介绍了Android新架构组件Navigation的使用,它能方便地处理Fragment之间的跳转和数据传递。通过引入依赖并设置NavHostFragment,开发者可以实现类似Activity跳转的功能。

2401

被折叠的 条评论
为什么被折叠?



