简体   繁体   English

如何使用导航类型 safeargs 插件将 Parcelable 类型的对象传递给 Fragment

[英]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.现在,除了integerstringinferredreference之外,您不能将安全参数用于其他类型,还有一个问题需要其他类型。

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 .您可以使用booleanreferenceintegerlongstringenumparcelable甚至可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.

相关问题 如何使用导航组件安全参数将列表作为参数传递给片段 - How to pass a List as an argument to a Fragment using Navigation Component safeargs 导航组件:将安全参数从活动传递到片段 - Navigation Component: pass safeArgs from activity to a fragment 使用导航组件安全参数将枚举作为参数传递给片段的正确方法是什么 - What is the correct way to pass an Enum as an argument to a Fragment using Navigation Component safeargs 如何在 Java 中的 Parcelable 类中打包对象类型 - how to parcel Object type in Parcelable class in Java 如何在使用导航组件和 safeArgs 从回收器视图导航到片段时添加共享元素转换? - How to add shared element transition when navigating from recycler view to a fragment while using navigation components and safeArgs? 如何使用parcelable传递BitmapShader类对象? - how to pass BitmapShader class object using parcelable? 如何使用宗地将CountDownTimer对象传递给Intent? - How to pass object of CountDownTimer into intent using parcelable? 无法将可拆分对象ArrayList传递给片段活动 - Not able to pass parcelable object ArrayList to a fragment activity 将可分割对象从片段传递到活动 - Pass an Parcelable Object from an Fragment to an activity 如何使用Parcelable在活动之间传递带有其他对象列表的对象? - How to pass object with List of other object between activities using Parcelable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM