📜  Android 中的 Jetpack 导航组件(1)

📅  最后修改于: 2023-12-03 15:13:21.338000             🧑  作者: Mango

Android 中的 Jetpack 导航组件

简介

Jetpack 导航组件是 Google 推出的一套开发工具,旨在简化 Android 应用程序的导航逻辑。它能够帮助开发人员轻松地添加导航,同时提高应用程序的可靠性和稳定性。

功能

Jetpack 导航组件提供了以下功能:

  • 导航图(NavGraph):定义应用程序中的组件和导航路径
  • 目标(Destination):定义屏幕或功能模块
  • 动作(Action):定义在不同目标之间导航的方式
  • 深度链接(Deep Link):使用户能够通过应用程序外部或链接将其导航到特定屏幕或模块
  • 状态安全性:确保应用程序在系统配置更改(如屏幕旋转)时保持状态
  • 对象接口(NavHost):定义目标在哪里显示
使用步骤
  1. 添加依赖

    在app/build.gradle文件中添加以下依赖:

    implementation "androidx.navigation:navigation-fragment-ktx:2.3.1"
    implementation "androidx.navigation:navigation-ui-ktx:2.3.1"
    
  2. 创建导航图

    在res文件夹中的navigation包下新建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">
    
        <fragment
            android:id="@+id/firstFragment"
            android:name="com.example.myapplication.FirstFragment"
            android:label="First Fragment"
            tools:layout="@layout/fragment_first">
            <action
                android:id="@+id/action_firstFragment_to_secondFragment"
                app:destination="@id/secondFragment" />
        </fragment>
    
        <fragment
            android:id="@+id/secondFragment"
            android:name="com.example.myapplication.SecondFragment"
            android:label="Second Fragment"
            tools:layout="@layout/fragment_second">
            <action
                android:id="@+id/action_secondFragment_to_thirdFragment"
                app:destination="@id/thirdFragment" />
        </fragment>
    
        <fragment
            android:id="@+id/thirdFragment"
            android:name="com.example.myapplication.ThirdFragment"
            android:label="Third Fragment"
            tools:layout="@layout/fragment_third" />
    
    </navigation>
    
  3. 设置导航

    在MainActivity中设置导航,如下:

    class MainActivity : AppCompatActivity() {
    
        private lateinit var navController: NavController
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            // 设置bottomNavigationView
            val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
            val appBarConfiguration = AppBarConfiguration(
                setOf(
                    R.id.homeFragment,
                    R.id.dashboardFragment,
                    R.id.notificationFragment
                )
            )
            setupActionBarWithNavController(navController, appBarConfiguration)
            bottomNavigationView.setupWithNavController(navController)
        }
    
        override fun onSupportNavigateUp(): Boolean {
            return navController.navigateUp() || super.onSupportNavigateUp()
        }
    }
    

以上代码将BottomNavigationViewNavController进行了绑定,使其能够与导航逻辑交互。

  1. 定义目标和动作

    使用导航图定义目标和动作,如上述的navigation.xml文件中所示。

  2. 在 fragment 中使用导航

    使用 view.findNavController() 方法返回一个 NavController 对象。

    在 fragment 中实现导航,如下:

    class FirstFragment : Fragment() {
    
        private lateinit var navController: NavController
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            // Inflate the layout for this fragment
            val view = inflater.inflate(R.layout.fragment_first, container, false)
    
            // 获取 NavController 对象
            navController = view.findNavController()
    
            // 绑定点击事件
            view.findViewById<Button>(R.id.button_first).setOnClickListener {
                navController.navigate(R.id.action_firstFragment_to_secondFragment)
            }
    
            return view
        }
    
    }
    

    在以上代码中,通过 navController.navigate 方法实现了从 FirstFragmentSecondFragment 的导航。

结论

Jetpack 导航组件提供了一种简单、快速、灵活的方式来定义和实现 Android 应用程序的导航逻辑,能够极大地提高应用程序的可维护性和可靠性。越来越多的开发人员正在使用 Jetpack 导航组件,推荐给广大 Android 开发人员使用。