[英]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:两个问题:
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
的内容,而是返回原始引用。
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.