前端基础组件和业务组件
We all have used fragments in our app and wrote fragment transactions many times according to our needs. So, the navigation component is another way to handle transactions but it provides few other benefits too.
我们所有人都在应用程序中使用了片段,并根据需要多次编写了片段事务。 因此,导航组件是处理事务的另一种方法,但是它也没有提供其他好处。
Some of them are:
他们之中有一些是:
- Handling back action correctly by default 默认情况下正确处理动作
- Default standard animations and transitions while switching between fragments 在片段之间切换时的默认标准动画和过渡
- Passing data between fragments (this is also my favorite). 在片段之间传递数据(这也是我的最爱)。
Okay, now let’s discuss how to integrate it into our app.
好的,现在让我们讨论如何将其集成到我们的应用程序中。
First, we’ve to add following dependencies in our app-level build.gradle
file:
首先,我们必须在应用程序级别的build.gradle
文件中添加以下依赖build.gradle
:
def nav_version = "2.3.0"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
The recommended way to navigate between destinations is to use the Safe Args Gradle plugin. This plugin generates simple object and builder classes that enable type-safe navigation and argument passing between destinations. To add Safe Args to our project, include the following classpath in our top-level build.gradle
file:
在目的地之间导航的推荐方法是使用Safe Args Gradle插件。 该插件生成简单的对象和构建器类,以启用类型安全的导航和在目标之间传递参数。 要将Safe Args添加到我们的项目中,请在顶级build.gradle
文件中包含以下类路径:
buildscript {
repositories {
google()
}
dependencies {
def nav_version = "2.3.0"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
}
}
Also, add this line to our app or module’s build.gradle
file:
另外,将此行添加到我们的应用程序或模块的build.gradle
文件中:
apply plugin: "androidx.navigation.safeargs"
There are three main compo