documentation讨论如何发送简单的整数和字符串。例如:
<argument
android:name="myIntArg"
android:defaultValue="255"
app:argType="integer" />
在原始 fragment 中:
val action = OriginFragmentDirections.myAction(myInt)
findNavController().navigate(action)
在目标 fragment 中:
val receivedInt = DestinationFragmentArgs.fromBundle(arguments).myIntArg
但是说而不是
myIntArg
,我想发送一个枚举( myEnumArg
)。我该怎么做?什么 app:argType
我会在我的论点中使用吗?
最佳答案
编辑:根据 Navigation 1.0.0-alpha08 release notes :
Safe Args supports Serializable objects, including Enum values. Enum types can set a default value by using the enum literal without the class name (e.g.
app:defaultValue="READ"
) b/111316353
所以现在这是可能的 - 您可以使用 Enum 类的名称(即
com.example.EnumClass
)或相对名称( .EnumClass
),它会自动将您的应用程序包名称添加到类名称之前。上一个答案:
当前版本的 Navigation (1.0.0-alpha07) 无法做到这一点,但 existing feature request被标记为固定,使用枚举作为参数的能力将在 alpha08 中可用
关于android - 使用导航组件安全参数将枚举作为参数传递给 fragment 的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53545518/