Request a FREE Quote

Android Jetpack包含一组库 & components to build a perfect application and the navigation component is part of it. Let’s learn about Android导航组件 以及如何与导航组件集成. The navigation component works by defining a graph of navigation destinations and actions. It provides set of APIs and tools that enable to define and manage navigation paths between app screens or destinations. 该库提供了许多好处,包括
Also Read: 是什么让Android即时应用如此受欢迎?
导航组件有三个模块:
Just add below code in the dependencies of module level build.gradle file.
def nav_version = "2.2.2"
// Java语言实现
实现“androidx.导航:navigation-fragment: $ nav_version”实现“androidx.导航:导航ui: $ nav_version”
// Kotlin
实现“androidx.导航:navigation-fragment-ktx: $ nav_version”
实现“androidx.导航:navigation-ui-ktx: $ nav_version”
First of all we need to create a file that will contain the navigation graph. 在res目录中执行以下步骤:
这将用nav_graph创建一个空白的资源文件.导航目录中的XML文件名.
For an example, here we have created a sample application with two different fragments named FirstFragment and SecondFragment. While clicking on the button of FirstFragment, It will navigate you to the SecondFragment.
We have defined fragments in the navigation graph as below.
A NavHost can be envisioned as a space where you can design your screen, 在NavController的帮助下, you can move between different screens within the NavHost.
Each screen within the NavHost is linked to a specific route, 哪个可以在屏幕之间导航. The following is an illustrative example of a NavHost :
val navController = memorbernavcontroller ()
NavHost(
navController = navController;
startDestination = "first_screen"
) {
可组合(“first_screen”){
// first screen
}
可组合(“second_screen”){
// second screen
}
}
这里根标签命名为navigation, there is app:startDestination parameter which contain id of first fragment.
This one defines that in the NavHostFragment, first fragment will be loaded automatically.
We have defined and action for first fragment with below attributes:
android: id =“@ + id / nav_first_fragment”
应用:目的地= " @ id / nav_second_fragment”
In every action there should be a unique id which we require to navigate to the destination.
There is the id of the second fragment in app:destination that we defined in the nav graph and using this action it will navigate to the second fragment.
After completion of these steps, switch to the design tab in nav_graph.Xml,它应该看起来像下面给出的图表.
Read More: Kotlin for Android
We have multiple ways to navigate using navigation component
button.setOnClickListener
{
findNavController().navigate(R.id.nav_second_fragment)
}
button.setOnClickListener(导航.createNavigateOnClickListener(右.id.nav_second_fragment, null))
button.setOnClickListener {
findNavController().navigate(R.id.action_first_to_second)
}
All we need to do is define the NavHostFragment and this widget will display different destinations that we defined in the navigation graph. 加载FirstFragment, we need to copy the below given code and paste it in the layout of the activity.
android: name = " androidx.navigation.fragment.NavHostFragment"
defines the NavHostFragment which is used by NavController
应用:defaultNavHost = " true "
is known as NavHost that intercepts and work as a back button.
应用:navGraph = " @navigation / app_navigation”
associates the NavHostFragment with a navigation graph. In this NavHostFragment, the navigation graph specifies all the destinations we can navigate to.
When you run the app, FirstFragment will be loaded automatically and click on button, 它将重定向到SecondFragment. While pressing the back button from SecondFragment, you will be redirected back to the FirstFragment.
That’s it, hope you have got the understanding of navigation architecture component.
Also Check: Revolutionizing 移动应用开发 with Kotlin Multiplatform
Are you struggling with navigation architecture component? You are at the right place, we serve a vast number of industries to provide Android应用开发解决方案. 与我们联系,从我们的 seasoned developers.