Android中的Jetpack导航组件

Android中的Jetpack导航组件

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. 该库提供了许多好处,包括

  1. 它自动处理片段事务
  2.  While navigating, type safety is there when passing information
  3. 导航UI模式,如底部导航 & navigation drawers are implementing with minimum effort.
  4. 动画和过渡都有默认行为
  5. 深度链接是一流的操作
  6. 默认情况下处理Up和back操作

Also Read: 是什么让Android即时应用如此受欢迎?

Overview

导航组件有三个模块:

  1. Navigation graph – It’s an XML resource; it is having all the navigation related data/information in one centralised place. 
  2. NavHostFragment – This important widget you have to add to your layout. It will show different destination from Navigation Graph.
  3. NavController – This one manages navigation of an application within a NavHost and keeps track of current position in navigation graph.   

Integration

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”

Navigation Graph

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.


    
Android中的Jetpack导航组件 - Login Process

NavigationHost

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

Navigation types

We have multiple ways to navigate using navigation component

  1. 使用目的地Id导航 
    For navigate, we can provide if of destination fragment like below.
    button.setOnClickListener
    {
    findNavController().navigate(R.id.nav_second_fragment)
    }
  2. ClickListener
    For views, we can use createNavigateOnClickListener() method as below
    button.setOnClickListener(导航.createNavigateOnClickListener(右.id.nav_second_fragment, null))
  3. 使用操作导航
    We can use id of the defined action in the first fragment in above graph as below
    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

Final Words

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.

Android,移动应用开发
,

Leave a Reply

您的电子邮件地址将不会被公布. 必填项被标记 *

Specially Thank you! for visiting.

你脑子里的任何计划?

Contact us 或打电话给我们获得免费的估计