简体   繁体   English

Jetpack导航的action有什么用?

[英]What is the use of the action of Jetpack navigation?

I am new to programming and I am confused about the action of android jetpack navigation, Since we can navigate directly to any other destination from any destination, why do we need action?我是编程新手,我对 android 喷气背包导航的动作感到困惑,既然我们可以从任何目的地直接导航到任何其他目的地,为什么我们需要采取行动?

If I have nav_graph.xml file like below.如果我有如下所示的 nav_graph.xml 文件。 I can navigate to ProfileFragment from MainFragment by call findNavController().navigate(R.id.profile_fragment) , so why do we need acion?我可以通过调用findNavController().navigate(R.id.profile_fragment)从 MainFragment 导航到 ProfileFragment,那么为什么我们需要行动? What's the point of it's existence?它存在的意义何在?

Can someone show me a simple example, it would be greatly appreciated.谁能给我看一个简单的例子,将不胜感激。

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/nav_graph"
        app:startDestination="@id/main_fragment">
    <fragment
            android:id="@+id/main_fragment"
            android:name="com.google.android.conditionalnav.MainFragment"
            android:label="fragment_main"
            tools:layout="@layout/fragment_main">
        <action
                android:id="@+id/navigate_to_profile_fragment"
                app:destination="@id/profile_fragment"/>
    </fragment>
    <fragment
            android:id="@+id/profile_fragment"
            android:name="com.google.android.conditionalnav.ProfileFragment"
            android:label="fragment_profile"
            tools:layout="@layout/fragment_profile"/>
</navigation>
  1. It is best to use action because it's safe to navigate with any runtime navigation error using navigation-safe-args plugin.最好使用操作,因为使用navigation-safe-args插件可以安全地导航任何运行时导航错误。

  2. Safe Args to pass data with type safety Safe Args 以类型安全的方式传递数据

  3. It show graphical linking representation of screens in design tab.它在设计选项卡中显示屏幕的图形链接表示。

An action is a logical connection between destinations.动作是目的地之间的逻辑连接。 Actions are represented in the navigation graph as arrows.操作在导航图中表示为箭头。 Actions usually connect one destination to another, though you can also create global actions that take you to a specific destination from anywhere in your app.操作通常将一个目的地连接到另一个目的地,但您也可以创建全局操作,将您从应用程序中的任何位置带到特定目的地。

With actions, you're representing the different paths that users can take through your app.通过操作,您代表了用户可以通过您的应用程序采取的不同路径。 Note that to actually navigate to destinations, you still need to write the code to perform the navigation.请注意,要实际导航到目的地,您仍然需要编写代码来执行导航。 This is covered in the Navigate to a destination section later in this topic.这将在本主题后面的导航到目标部分中介绍。

Reference: Navigation Actions参考: 导航操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM