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:
-
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.
-
NavHostFragment (Layout XML view) - This is a special widget you add to your layout. It displays different destinations from your Navigation Graph.
-
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.
使用
-
依赖库
//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")
-
创建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
表示动作
-
-
创建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" />//文件名
-
其他view如BottomNavigationView、AppBar、NavigationView支持
navigation
组件的view有响应协同扩展函数setUpWithNavController
附注
问题点:
-
结合BottomNavigationView时候Fragment的返回管理问题
-
fragment对象实例单例对象的问题
navigate中 fm的replace,而不是hide修复示例
-
IDE支持问题,navigation创建、fragment标签使用,以及graph,navigation的生效
-
对于组件化的支持,有潜力可能
-
使用扩展方式支持bottomNavigationView配合fragment的时候,单例对象问题。
-
注意contrainter和graph的使用
-
-
bottomNavigationView中menu的item的it和graph中的id一致性(id 不是name)
fragment生命周期图 图解Activity&Fragment生命周期 - 简书