[英]How to pass object of type Parcelable to a Fragment using Navigation type safeargs plugin
I am rewriting my simple UI app to use Navigation architecture component, I need to pass a Pojo that implements Parcelable, have not seen any doc on how to do that.我正在重写我的简单 UI 应用程序以使用导航架构组件,我需要传递一个实现 Parcelable 的 Pojo,还没有看到任何关于如何做到这一点的文档。
Any help would be appreciated.任何帮助,将不胜感激。
Since safe-args-gradle-plugin:1.0.0-alpha03
you can use Parcelable
objects by using their fully qualified class name:由于safe-args-gradle-plugin:1.0.0-alpha03
可以使用Parcelable
通过使用其完全限定类名称的对象:
<argument
android:name="item"
app:argType="com.example.app.model.Item"/>
Parcelable arguments are now supported, using a fully qualified class name for app:type.现在支持 Parcelable 参数,使用 app:type 的完全限定类名。 The only default value supported is "@null" ( https://issuetracker.google.com/issues/79563966 )支持的唯一默认值是“@null”( https://issuetracker.google.com/issues/79563966 )
Source: https://developer.android.com/jetpack/docs/release-notes来源: https : //developer.android.com/jetpack/docs/release-notes
To support nullability one has to use android:defaultValue="@null"
with app:nullable="true"
.为了支持可空性,必须使用android:defaultValue="@null"
和app:nullable="true"
。
I know the answer is already there but this may help someone.我知道答案已经存在,但这可能会对某人有所帮助。 Code snippet
In build.gradle add this dependancy在 build.gradle 添加这个依赖
ext{
...
navigation_version = '1.0.0-alpha11'
}
dependencies {
...
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:$navigation_version"
}
In app/build.gradle在 app/build.gradle 中
apply plugin: 'androidx.navigation.safeargs'
...
In Navigation graph在导航图中
<fragment
android:id="@+id/source_fragment_id"
android:name="app.test.SourceFragment"
android:label="@string/source_fragment_label"
tools:layout="@layout/source_fragment_layout">
<action
android:id="@+id/action_source_fragment_to_destination_fragment"
app:destination="@id/destination_fragment_id"
...
/>
</fragment>
<fragment
android:id="@+id/destination_fragment_id"
android:name="app.test.DestinationFragment"
android:label="@string/destination_fragment_label"
tools:layout="@layout/destination_fragment_layout">
<argument
android:name="variableName"
app:argType="app.test.data.model.CustomModel" />
...
</fragment>
Note: CustomModel should be Parcelable or Serializable.注意:CustomModel 应该是 Parcelable 或 Serializable。
When navigating to this DestinationFragment from SourceFragment从 SourceFragment 导航到此 DestinationFragment 时
val direction = SourceFragmentDirections.ActionSourceFragmentToDestinationFragment(customModel)
findNavController().navigate(direction)
Now retrieving the value from bundle in DestinationFragment现在从 DestinationFragment 中的 bundle 中检索值
...
import app.test.DestinationFragmentArgs.fromBundle
class DestinationFragment : Fragment() {
val variableName by lazy {
fromBundle(arguments!!).variableName
}
...
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Log.e(DESTINATION_FRAGMENT_TAG,"onCreateView")
//Can use CustomModel variable here i.e. variableName
}
}
Right now you can't use safe args with types apart from integer , string , inferred and reference , there's an issue opened asking for other types.现在,除了integer 、 string 、 inferred和reference之外,您不能将安全参数用于其他类型,还有一个问题需要其他类型。
What you can do now is to normally pass a bundle when using the navigate() method to navigate to a destination:您现在可以做的是在使用navigate()方法导航到目的地时通常传递一个包:
var bundle = bundleOf("amount" to amount)
view.findNavController().navigate(R.id.confirmationAction, bundle)
And you can use the usual getArguments
(or just arguments in kotlin) to retrieve that:您可以使用通常的getArguments
(或 kotlin 中的参数)来检索:
val tv = view.findViewById(R.id.textViewAmount)
tv.text = arguments.getString("amount")
You can use boolean
, reference
, integer
, long
, string
, enum
, parcelable
and even serializable
.您可以使用boolean
、 reference
、 integer
、 long
、 string
、 enum
、 parcelable
甚至可serializable
。 But forget about the last one ;-)但是忘记最后一个 ;-)
Better use the latest plugin version safe-args-gradle-plugin:1.0.0-alpha08
and specify the fully qualified classname:最好使用最新的插件版本safe-args-gradle-plugin:1.0.0-alpha08
并指定完全限定的类名:
<fragment
...>
<argument
android:name="data"
app:argType="com.example.ParcelableData" />
</fragment>
from your从你的
package com.example
data class ParcelableData(val content: String) : Parcelable { ... }
And you can send arrays of all the argType
s:您可以发送所有argType
的数组:
<argument
android:name="data"
app:argType="string[]" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.