4-1 Navigation的学习使用

Navigation的学习使用

JetPack中用于页面导航的重要组件

优势特点:

  • Automatic handling of fragment transactions

  • Correctly handling up and back by default

  • Default behaviors for animations and transitions

  • Deep linking as a first class operation

  • Implementing navigation UI patterns (like navigation drawers and bottom nav) with little additional work

  • Type safety when passing information while navigating

  • Android Studio tooling for visualizing and editing the navigation flow of an app

基础概念

The Navigation Component consists of three key parts, working together in harmony. They are:

  1. Navigation Graph (New XML resource) - This is a resource that contains all navigation-related information in one centralized location. This includes all the places in your app, known as destinations, and possible paths a user could take through your app.

  2. NavHostFragment (Layout XML view) - This is a special widget you add to your layout. It displays different destinations from your Navigation Graph.

  3. NavController (Kotlin/Java object) - This is an object that keeps track of the current position within the navigation graph. It orchestrates swapping destination content in the NavHostFragment as you move through a navigation graph.

使用

  1. 依赖库

    //module下的build.gradle
    implementation "androidx.navigation:navigation-ui:$navigation_version"
    implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
    implementation "androidx.navigation:navigation-fragment:$navigation_version"
    implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
    implementation "androidx.navigation:navigation-runtime:$navigation_version"

    如需要使用plugin配置safe args

    //module 的gradle
    apply plugin: 'androidx.navigation.safeargs.kotlin'//kotlin的module,如果是java的module,类似的plugin
    apply plugin: 'androidx.navigation.safeargs'//java的module添加这样的

    然后再在project的gradle下添加

    //最好保证不同module和project的classpath的 navigation的版本一致
    classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$navigation_version")
  2. 创建graph 并添加需要的fragment, 文件名graph_main

    <?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_graph_main"
        app:startDestination="@id/homeFragment">
    ​
        <fragment
            android:id="@+id/homeFragment"
            android:name="com.cniao5.home.HomeFragment"
            android:label="HomeFragment"
            tools:layout="@layout/fragment_home" />
        <fragment
            android:id="@+id/courseFragment"
            android:name="com.cniao5.course.CourseFragment"
            android:label="CourseFragment"
            tools:layout="@layout/fragment_course" />
        <fragment
            android:id="@+id/studyFragment"
            android:name="com.cniao5.study.StudyFragment"
            android:label="StudyFragment"
            tools:layout="@layout/fragment_study" />
        <fragment
            android:id="@+id/mineFragment"
            android:name="com.cniao5.mine.MineFragment"
            android:label="MineFragment"
            tools:layout="@layout/fragment_mine" >
            <action id="@+id/action_mine_to_other" 
                app:destination="@id/courseFragment"
                app:enterAnim=""
                app:exitAnim=""
                app:launchSingleTop="true"
                app:popEnterAnim=""
                app:popUpTo="@id/studyFragment"//结对存在
                app:popUpToInclusive="true">
            </action>
        </fragment>
    ​
    </navigation>

    路径在res/navigation下,自建对应navigation即可。

    • 根节点navigation 子节点fragment

    • 关键属性startDestination

    • action表示动作

  3. 创建nav host fragment 的容器,关联graph

    <!-- 重点在于 name 和 defalutNavHost navGraph 三个属性的配置 -->
    <fragment
                android:id="@+id/fcv_main"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:defaultNavHost="true"
                app:layout_constraintBottom_toTopOf="@id/bnv_main"
                app:layout_constraintTop_toTopOf="parent"
                app:navGraph="@navigation/graph_main" />//文件名
  4. 其他view如BottomNavigationView、AppBar、NavigationView支持navigation组件的view有响应协同扩展函数setUpWithNavController

附注

问题点:

  1. 结合BottomNavigationView时候Fragment的返回管理问题

  2. fragment对象实例单例对象的问题

    navigate中 fm的replace,而不是hide修复示例

  3. IDE支持问题,navigation创建、fragment标签使用,以及graph,navigation的生效

  4. 对于组件化的支持,有潜力可能

  5. 使用扩展方式支持bottomNavigationView配合fragment的时候,单例对象问题。

    • 注意contrainter和graph的使用

  6. bottomNavigationView中menu的item的it和graph中的id一致性(id 不是name)

fragment生命周期图 图解Activity&Fragment生命周期 - 简书

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值