1.Introduce
导航组件简化了导航的实现,也可以实现导航流程的可视化。navigation库提供了一系列便捷:
- 自动处理fragment transactions
- Correctly handling up and back by default
- Default behaviors for animations and transitions
- Deep linking as a first class operation
- 只需要一点点额外工作就可以实现导航UI模板(就像navigation drawers 和 bottom nav)
- Type safety when passing information while navigating
- Android studio提供了app导航的可视化和导航流的编辑
BEHOLD:
2.Getting Start
$ git clone https://github.com/googlecodelabs/android-navigation
Navigation概述
导航组件包括三个关键部分:
- Navigation Graph(新的xml资源) :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(XML布局视图):This is a special widget you add to your layout. It displays different destinations from your Navigation Graph.
- NavController(一个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.
3.Introduce the Navigation Graph
Destinations(目的地)
导航组件引入了Destination的概念。Destination是应用中可以导航到达的任意位置,一般来讲是一个fragment或者activity。These are supported out of the box, but you can also make your own custom destination types if needed。
Navigation Graph(导航图)
导航图是一种新的资源类型,定义了应用中用户可以使用的所有路径。它直观地显示了一个目的地(Destination)可以到达的所有其他的目的地。下面是练习中导航的起始状态:
Navigation Editor(导航编辑器)
- 打开 res/navigation/mobile_navigation.xml
- 点Design
destination之间的箭头叫做action,这个我们晚一点讨论。 -
点击一个destination查看属性
4.查看action的属性,点击箭头就可以了
分析navigation的XML文件
<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/home_dest">
<!-- ...tags for fragments and activities here -->
</navigation>
<navigation>是导航图的根节点
<navigation>包含一个或多个destination,<activity>或<fragment>
-
app:startDestination定义app打开时默认加载的destination
下面是一个fragment destination<fragment android:id="@+id/flow_step_one_dest" android:name="com.example.android.codelabs.navigation.FlowStepFragment" tools:layout="@layout/flow_step_one_fragment"> <argument .../> <action android:id="@+id/next_action" app:destination="@+id/flow_step_two_dest"> </action> </fragment>
android:id 就是一个id
android:name 引用的fragment的全路径
tools:layout 导航图中显示的布局哦
4.给Navigation Graph添加一个Destination
- 打开mobile_navigation.xml,进入design模式
- 点击new Destination,选择settings_fragment
5.使用导航图进行导航
Activity和Navigation
导航组件遵循着Principles of Navigation中叙述的指导思想。指导中提到应该用activity作为app的入口,而且activity中应该包含全局导航,比如bottom nav,
比较下来, fragments will be the actual destination-specific layouts.
To get this all to work,需要让activity包含一个叫做NavHostFragment的特殊部件。一个NavHostFragment会根据导航图中的导航交换不同的fragment destination。
举个栗子:
<LinearLayout
.../>
<androidx.appcompat.widget.Toolbar
.../>
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/mobile_navigation"
app:defaultNavHost="true"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
.../>
</LinearLayout>
- 以上是一个activity的layout,包含了全局导航:一个bottom nav和一个toolbar
- android:name="androidx.navigation.fragment.NavHostFragment"和app:defaultNavHost="true"把系统后退按钮连接到NavHostFragment
- app:navGraph="@navigation/mobile_navigation"将HavHostFragment和导航图关联起来,导航图中定义了在NavHostFragment中可以导航的所有destinatioin
NavController
当用户进行一些操作,比如点击了某个按钮,就需要触发导航命令,NavController就是触发NavHostFragment交互fragment的指定类。
// Command to navigate to flow_step_one_dest
findNavController().navigate(R.id.flow_step_one_dest)
这里传入destination的id或者action的id都可以哦。
NavController很强大的,当调用navigate()或者popBackStack()时,它会把这些命令根据destination的来(from)或去(to)转换成适当的框架操作。比如,当对一个activity destination调用navigation()时,NavController会调用startActivity()。
获取与NavHostFragment相关的NavController有几个途径。在Kotlin中,根据在fragment,activity或者view中获取NavController,推荐从以下选择:
Fragment.findNavController()
View.findNavController()
-
Activity.findNavController(viewId: Int)
注意:NavController是和NavHostFragment相关的,所以不论使用哪个方法,都必须保证fragment,view,或者view id是NavHostFragent本身,或者有NavHostFragment作为Parent,否则会诞生IllegalStateException。
使用NavContriller导航去一个Destination
是时候表演真正的技术了,现在将要点击“Navigate To Destination”按钮导航去flow_step_one_dest
destination(FlowStepFragment):
- 打开HomeFragment.kt
- 在onViewCreated()中搞navigate_destination_button
val button = view.findViewById<Button>(R.id.navigate_destination_button)
button?.setOnClickListener {
findNavController().navigate(R.id.flow_step_one_dest)
}
或者
val button = view.findViewById<Button>(R.id.navigate_destination_button)
button?.setOnClickListener( Navigation.createNavigateOnClickListener(R.id.flow_step_one_dest, null)
)
都可以