
Jetpack Navigation 是 Android 用来管理页面跳转和返回栈的组件。它使用方法的大概逻辑如下定义目标页面常用Fragment也可以跳转Activity、Dialog定义导航图nav_graph.xml可以声明起始页面startDestination参数argumentdeep linkaction动画等在Activity中放置NavHostFragment作为页面的容器。一般使用FragmentContainerView实现作为容器在对应的Fragment类中写明触发跳转逻辑使用findNavController().navigate(...)等等方法添加依赖dependencies { implementation(androidx.navigation:navigation-fragment-ktx:2.9.5) implementation(androidx.navigation:navigation-ui-ktx:2.9.5) }定义导航图?xml version1.0 encodingutf-8? navigation xmlns:androidhttp://schemas.android.com/apk/res/android xmlns:apphttp://schemas.android.com/apk/res-auto android:idid/nav_graph app:startDestinationid/homeFragment !-- 首页 -- fragment android:idid/homeFragment android:namecom.example.navigationdemo.HomeFragment android:label首页 !-- 跳转动作 -- action android:idid/action_home_to_detail app:destinationid/detailFragment / /fragment !-- 详情页 -- fragment android:idid/detailFragment android:namecom.example.navigationdemo.DetailFragment android:label详情页 !-- 页面参数 -- argument android:nameuserName app:argTypestring / /fragment /navigation放置NavHostFragmentactivity_main.xml?xml version1.0 encodingutf-8? androidx.fragment.app.FragmentContainerView xmlns:androidhttp://schemas.android.com/apk/res/android xmlns:apphttp://schemas.android.com/apk/res-auto android:idid/nav_host_fragment android:nameandroidx.navigation.fragment.NavHostFragment android:layout_widthmatch_parent android:layout_heightmatch_parent app:navGraphnavigation/nav_graph app:defaultNavHosttrue /Fragment中跳转逻辑HomeFragmentfragment_home.xml?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:gravitycenter android:orientationvertical android:layout_widthmatch_parent android:layout_heightmatch_parent Button android:idid/btn_jump android:layout_widthwrap_content android:layout_heightwrap_content android:text跳转到 DetailFragment / /LinearLayout对应的类HomeFragment.ktclass HomeFragment : Fragment(R.layout.fragment_home) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val btnJump view.findViewByIdButton(R.id.btn_jump) btnJump.setOnClickListener { // 传递参数 val bundle bundleOf( userName to Tom ) // 执行跳转 findNavController().navigate( R.id.action_home_to_detail, bundle ) } } }DetailFragmentfragment_detail.xml?xml version1.0 encodingutf-8? FrameLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:layout_widthmatch_parent android:layout_heightmatch_parent TextView android:idid/tv_name android:layout_gravitycenter android:textSize22sp android:layout_widthwrap_content android:layout_heightwrap_content / /FrameLayout对应的类DetailFragment.ktclass DetailFragment : Fragment(R.layout.fragment_detail) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) // 接收参数 val userName requireArguments().getString(userName) val tvName view.findViewByIdTextView(R.id.tv_name) tvName.text userName $userName } }其他用法deep link、safe args、 动态设置NavGraph等等优缺点需要在 nav_graph.xml 提前配置所有页面节点不易维护. 只有当时写得人容易看懂而后续维护的人会很费时间。 而且页面多起来时会导致文件过于臃肿需要在nav_graph.xml 配置固定的 startDestination启动节点不够灵活Fragment 类型的路由导航使用的replace会使得页面频繁重建体验不够友好Fragment singleTop启动模式即便目标页在栈顶依然会销毁后再重建体验不友好