简体   繁体   English

如何在 android 导航组件中传递参数名称?

[英]How to pass argument name in android navigation component?

I'm using a navigation graph to navigate between fragments.我正在使用导航图在片段之间导航。 Everything works fine, but I wonder how to pass argument name in an elegant way.一切正常,但我想知道如何以优雅的方式传递参数名称。

Here is my code snippet from navSheet.xml :这是我来自navSheet.xml的代码片段:

A source fragment action:源片段动作:

    <action
        android:id="@+id/action_go_to_B"
        app:destination="@id/id_fragment_b">
        <argument
            android:name="@string/fragment_b_arg_key_name"
            app:argType="integer"/>
    </action>

A destination fragment:目标片段:

<fragment
    android:id="@+id/id_fragment_b"
    android:name="pl.asd.FragmentB"
    tools:layout="@layout/fragment_b">

    <argument
        android:name="@string/fragment_b_arg_key_name"
        app:argType="integer"/>
</fragment>

I store my argument key in string.xml :我将参数键存储在string.xml中:

<string name="fragment_b_arg_key_name" translatable="false">arg_fragment_b</string>

And at the end I try to get my argument in destination fragment:最后我试图在目标片段中得到我的论点:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            selectedId = it.getInt(getString(R.string.fragment_b_arg_key_name))
        }
    }

The problem is with android:name="@string/fragment_b_arg_key_name" .问题在于android:name="@string/fragment_b_arg_key_name" I've seen on the debugger that there is an incorrect bundle key.我在调试器上看到有一个不正确的捆绑密钥。 It should be like:它应该是这样的:

Bundle[{arg_fragment_b=-730732511}]

but I get:但我得到:

Bundle[{@string/fragment_b_arg_key_name=-730732511}]

Two questions:两个问题:

  1. What's wrong with using a @string/fragment_b_arg_key_name in navSheet.xml so it not returns the content of @string/fragment_b_arg_key_name but raw reference.在 navSheet.xml 中使用@string/fragment_b_arg_key_name有什么问题,因此它不会返回@string/fragment_b_arg_key_name的内容,而是返回原始引用。

  2. How to not hard-code arguments name that I use in Fragments, navSheet.xml and other classes?如何不对我在 Fragments、 navSheet.xml和其他类中使用的 arguments 名称进行硬编码? How to store such keys in one place?如何将这些密钥存储在一个地方?

What's wrong with using a @string/fragment_b_arg_key_name in navSheet.xml so it not returns the content of @string/fragment_b_arg_key_name but raw reference.在 navSheet.xml 中使用 @string/fragment_b_arg_key_name 有什么问题,因此它不会返回 @string/fragment_b_arg_key_name 的内容,而是返回原始引用。

Simply because Android Navigation component does not support that feature to parse the string.仅仅是因为 Android 导航组件不支持该功能来解析字符串。 Every string put in android:name will become the raw id for the bundle. android:name 中的每个字符串都将成为捆绑包的原始 ID。 You can issue this in https://issuetracker.google.com/issues您可以在https://issuetracker.google.com/issues中发出此问题

However, there is better solution for this.但是,对此有更好的解决方案。 See below.见下文。

How to not hard-code arguments name that I use in Fragments, navSheet.xml and other classes?如何不对我在 Fragments、navSheet.xml 和其他类中使用的 arguments 名称进行硬编码? How to store such keys in one place?如何将这些密钥存储在一个地方?

Use safe args https://developer.android.com/jetpack/androidx/releases/navigation#safe_args使用安全参数https://developer.android.com/jetpack/androidx/releases/navigation#safe_args

You can change your codes like below您可以更改您的代码如下

FragmentA片段A

findNavController().navigate(R.id.id_fragment_b, FragmentBArgs(id).toBundle())

FragmentB片段B

private val args : FragmentBArgs by navArgs()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    selectedId = args.arg_fragment_b
}

in navSheet.xml:在 navSheet.xml 中:

<argument
    android:name="arg_fragment_b"
    app:argType="integer"/>

So, no need to store the keys, because safeargs will generate the arguments for you, and by default it is non-nullable.因此,无需存储密钥,因为 safeargs 将为您生成 arguments,默认情况下它是不可为空的。

To put the argument as Nullable, use app:nullable="true"要将参数设置为 Nullable,请使用app:nullable="true"

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

相关问题 如何将 function 作为参数传递给目标片段 - Android 导航组件 - How to pass a function as an argument to a destination fragment - Android Navigation Component 如何使用导航组件安全参数将列表作为参数传递给片段 - How to pass a List as an argument to a Fragment using Navigation Component safeargs 如何使用 Kotlin DSL 在 Android 导航中传递参数 - How to pass argument in Android Navigation using Kotlin DSL Android 导航架构组件:如何将捆绑数据传递给 startDestination - Android Navigation Architecture Component: How to pass bundle data to startDestination Android 导航组件:在片段中传递值(参数) - Android Navigation Component : Pass value (arguments) in fragments 如何使用底部导航视图和 Android 导航组件将参数传递给片段? - How to pass arguments to a fragment using bottom navigation view and Android Navigation component? 如何用Android Navigation Component实现Navigation Drawer - How to implement Navigation Drawer with Android Navigation Component 将 Serializable 用作 Android 导航组件的深层链接参数 - Using Serializable as deep link argument with Android Navigation Component Android 导航组件通过 safeArgs 和深度链接传递参数 - Android navigation component passing argument with safeArgs and deep link Android 导航组件 - 外部包含图形接收一个参数 - Android Navigation Component - external included graph receiving one argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM