1.导包

implementation ‘androidx.navigation:navigation-fragment-ktx:2.3.1’
implementation ‘androidx.navigation:navigation-ui-ktx:2.3.1’

2.1 建立 Navigation 入口

在Activity布局文件中建立Fragmeng,必须位NavHostFragment

    <fragmentandroid:id="@+id/nav_register_frag"android:fitsSystemWindows="true"android:name="androidx.navigation.fragment.NavHostFragment"android:layout_width="match_parent"app:defaultNavHost="true"android:layout_height="match_parent"app:navGraph="@navigation/nav_register" />
  • app:defaultNavHost=“true”
    设置 Navigation的返回模式,true表明Fragment之间按返回键 返回上一个Fragment;false 表示直接退出Activity。
  • app:navGraph=“@navigation/nav_register”
    指定的 Navigation xml 文件。

2.2 新建 Navigation 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/nav_register"app:startDestination="@id/enterPhoneFragment"><fragmentandroid:id="@+id/enterPhoneFragment"android:name="com.pqtel.pqsecuritysaw.ui.reg.EnterPhoneFragment"android:label="EnterPhoneFragment"tools:layout="@layout/frag_enter_phone"><actionandroid:id="@+id/action_enterPhoneFragment_to_enterCodeFragment"app:destination="@id/enterCodeFragment" /></fragment><fragmentandroid:id="@+id/enterCodeFragment"android:name="com.pqtel.pqsecuritysaw.ui.reg.EnterCodeFragment"android:label="EnterCodeFragment"tools:layout="@layout/frag_enter_code"><actionapp:popUpTo="@id/enterCodeFragment"app:popUpToInclusive="true"android:id="@+id/action_enterCodeFragment_to_enterPwdFragment"app:destination="@id/enterPwdFragment" /></fragment><fragmentandroid:id="@+id/enterPwdFragment"android:name="com.pqtel.pqsecuritysaw.ui.reg.EnterPwdFragment"android:label="EnterPwdFragment"tools:layout="@layout/frag_enter_pwd" />
</navigation>
  • app:startDestination=“@id/enterPhoneFragment”
    主入口,也就是首页面必须指定 否则报错。
  • app:destination=“@id/enterCodeFragment”
    Action 跳转动作 跳转的目的Fragment
  • app:popUpTo=“@id/enterCodeFragment”
    app:popUpToInclusive=“true”

    一般一起使用如果设置了app:defaultNavHost=“true”
    app:popUpTo 填自己即把自己出栈
    理论上的跳转A-B-C,按下返回时候是C-B-A
    若B到C的Action设置了这两条属性,则返回变成了C-A,B被出栈了。

2.3 BottomNavigation与Navigation

可以用此组合替代 viewpager和Tablayout
activity.xml

 <com.google.android.material.bottomnavigation.BottomNavigationViewandroid:id="@+id/nav_view"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginStart="0dp"android:layout_marginEnd="0dp"android:background="?android:attr/windowBackground"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:menu="@menu/bottom_nav_menu" /><fragmentandroid:id="@+id/nav_host_fragment"android:name="androidx.navigation.fragment.NavHostFragment"android:layout_width="match_parent"android:layout_height="match_parent"app:defaultNavHost="true"app:layout_constraintBottom_toTopOf="@id/nav_view"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:navGraph="@navigation/mobile_navigation" />

navigation.xml

    <fragmentandroid:id="@+id/navigation_home"android:name="com.pqtel.navtest.ui.home.HomeFragment"android:label="@string/title_home"tools:layout="@layout/fragment_home" /><fragmentandroid:id="@+id/navigation_dashboard"android:name="com.pqtel.navtest.ui.dashboard.DashboardFragment"android:label="@string/title_dashboard"tools:layout="@layout/fragment_dashboard" /><fragmentandroid:id="@+id/navigation_notifications"android:name="com.pqtel.navtest.ui.notifications.NotificationsFragment"android:label="@string/title_notifications"tools:layout="@layout/fragment_notifications" />

注意这里的fragment id 需要与menuitem id 一一对用,否则无法跳转

2.4 Navigation 跳转

NavHostFragment
.findNavController(this)
.navigate(id)

Android Navigation 使用总结相关推荐

  1. Android Navigation Drawer(导航抽屉)

    Google I/O 2013 Android 更新了Support库,新版本的Support库中新加入了几个比较重要的功能. 添加 DrawerLayout 控件,支持创建  Navigation ...

  2. 有关Android导览(Android Navigation component)

    文章目录 小结 有关Android导览(Android Navigation component) 碰到的问题 参考 小结 在使用Android导览(Android Navigation compon ...

  3. Android—Navigation的使用

    Navigation是Fragment的一个容器,用于管理Fragment. 我们可以通过它实现Fragment的跳转以及传值等操作. 1.先写Fragment的xml文件 <?xml vers ...

  4. android navigation bar高度,Android获取屏幕真实高度包含NavigationBar(底部虚拟按键)

    释放双眼,带上耳机,听听看~! public int getScreentHeight() { int heightPixels; WindowManager w = this.getWindowMa ...

  5. Android Navigation 组件(基础篇)

    一.前言 在日常开发中,越来越多的会使用到一个activity嵌套多个fragment的场景,典型的例子就是app的首页,一般都会由一个activity+多个Fragment组成的底部导航界面,那对于 ...

  6. Android Navigation与BottomNavigationView实现底部导航栏

    底部导航栏 一.效果图 二.实现 1.创建Fragment以及布局文件 2.添加FragmentContainerView和BottomNavigationView两个控件 3.配置xml资源文件 4 ...

  7. Android | navigation入门详解

    本文来自博客园,作者:{观心静},转载请注明原文链接: {https://www.cnblogs.com/guanxinjing/} 本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此 ...

  8. Android - Navigation

    文章目录 1. 创建Fragment 2. 添加Navigation 3. activity_main中添加NavHostFragment容器 4. MainActivity.java 5. 两个Fr ...

  9. Android Navigation使用

    简介 Navigation导航编辑器旨在简化Android开发中导航的实现,可以帮助我们很好的处理Activity和fragment之间通过FragmentTransaction交互的复杂性,也可以很 ...

  10. Android - Navigation组件

    Navigation 组件使用入门 设置你的环境 注意:Navigation 组件需要 Android Studio 3.3 或更高版本,并且依赖于 Java 8 语言功能. 如需在您的项目中添加 N ...

最新文章

  1. css深入浅出 宽度和高度
  2. 特殊的求和(函数和循环)
  3. 阿里巴巴对Java编程【并发处理】的规约
  4. MVVM模式于MVP模式
  5. 构建之法现代软件概述
  6. 15万个监控摄像头被黑,医院、学校、监狱都被看得一清二楚
  7. Centos/Linux下如何查看网关地址/Gateway地址
  8. 数组实例的includes()方法
  9. Kafka使用经验小结
  10. Java毕业设计-医院药品管理系统
  11. 百度文库会员制度悄然上线
  12. CUDA驱动版本与运行版本不匹配问题详解
  13. Flash退出历史舞台后,Web端3D会迎来怎样的发展?
  14. 牛逼,一个开源,高隐私,自架自用的聚合搜索引擎
  15. 人们为什么把电子计算机叫电脑,《计算机王国》.pdf
  16. Rabbitmq用户角色
  17. 毫无疑问计算机犯罪是一个很严重的问题英语,英语四级作文高分句型
  18. 杜克计算机工程本科专业申请,Duke-C同学斩获2015 杜克大学 计算机工程硕士Offer一枚...
  19. k8s 不宕机滚动发布实战笔记
  20. 【Golang】-微信二次分享及Js Sdk签名工具

热门文章

  1. javaScript 多线程并行编程 施工中~
  2. 2021-2027全球与中国重量稀释仪市场现状及未来发展趋势
  3. 亚商投资顾问早餐FM/0307稳增长、稳就业、稳物价
  4. 侵犯公民个人信息: “两高”首次出台司法解释 打击大数据征信乱象
  5. 缅怀TOC理论的创建者高德拉特先生
  6. 一天一篇mysql之一:认识mysql
  7. 浪潮服务器管理口地址linux系统,Linux-HikvisionOS系统安装手册-管理口安装[1].pdf
  8. myeclipse10激活注册码生成器代码
  9. vs2015中无法设置Qt版本,qt project setting 是灰色,Qt无法编译ui文件
  10. 《Emotion Cause Events: Corpus Construction and Analysis》