繁体   English   中英

Jetpack导航的action有什么用?

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

我是编程新手,我对 android 喷气背包导航的动作感到困惑,既然我们可以从任何目的地直接导航到任何其他目的地,为什么我们需要采取行动?

如果我有如下所示的 nav_graph.xml 文件。 我可以通过调用findNavController().navigate(R.id.profile_fragment)从 MainFragment 导航到 ProfileFragment,那么为什么我们需要行动? 它存在的意义何在?

谁能给我看一个简单的例子,将不胜感激。

<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. 最好使用操作,因为使用navigation-safe-args插件可以安全地导航任何运行时导航错误。

  2. Safe Args 以类型安全的方式传递数据

  3. 它在设计选项卡中显示屏幕的图形链接表示。

动作是目的地之间的逻辑连接。 操作在导航图中表示为箭头。 操作通常将一个目的地连接到另一个目的地,但您也可以创建全局操作,将您从应用程序中的任何位置带到特定目的地。

通过操作,您代表了用户可以通过您的应用程序采取的不同路径。 请注意,要实际导航到目的地,您仍然需要编写代码来执行导航。 这将在本主题后面的导航到目标部分中介绍。

参考: 导航操作

暂无
暂无

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

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