简体   繁体   English

如何将不同的参数传递给片段[导航] Android

[英]How pass different args to fragment [navigation] Android

I have 4 fragment, A,B,C,D FROM A,B,C I can navigate to D fragment, I want to pass different args from each fragment like,我有 4 个片段,A,B,C,D FROM A,B,C 我可以导航到 D 片段,我想从每个片段传递不同的参数,例如,

A.Direction.actionNavigation.D(int,String)
B.Direction.actionNavigation.D(float,Bool)
C.Direction.actionNavigation.D(double,String) and etc..

I can set all six args in D fragment argument but in this case I should pass all six variable on each direction.我可以在 D 片段参数中设置所有六个参数,但在这种情况下,我应该在每个方向上传递所有六个变量。 so is there other way to pass directly the args which i want on specific direction and then retrieve it in D fragment那么有没有其他方法可以直接传递我想要在特定方向上的参数,然后在 D 片段中检索它

Assuming you are Navigation Component, you need to make all the arguments that D Fragment demands to be nullable.假设您是导航组件,您需要使 D 片段要求的所有 arguments 都可以为空。 In this way, you can forward only the particular arg from a particular fragment of your need to the D Fragment.这样,您可以仅将特定 arg 从您需要的特定片段转发到 D 片段。 For doing the same, you need to make changes to the xml file regarding the navigation component, which in general is called nav_graph.xml.为此,您需要更改有关导航组件的 xml 文件,通常称为 nav_graph.xml。

<argument
            android:name="fromAtoD"
            app:argType="string"
            app:nullable="true"/>

The above changes need to be made to achieve your needs.需要进行上述更改才能满足您的需求。 You need to make null checks to avoid the code from crashing您需要进行 null 检查以避免代码崩溃


Another approach to this, will be using Intent and passing bundles within Intent, which is not recommended neither is safe.另一种方法是使用 Intent 并在 Intent 中传递包,不建议这样做,也不安全。

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

相关问题 如何将数据从活动传递到导航抽屉android中的片段 - How to pass data from an activity to a fragment inside a navigation drawer android Android AlertDialog如何传递参数 - Android AlertDialog how to pass args Android Studio 如何在使用导航视图时将数据从片段传递到片段? - Android Studio How do I pass data from fragment to fragment when a navigation view is being used? Android:导航抽屉中每个片段的不同工具栏 - Android: different toolbar for each fragment in navigation drawer 如何在旧片段上滑动时为 Android 导航架构片段设置动画? - How to animate Android Navigation Architecture fragment as sliding over old fragment? Android 中具有相同导航抽屉的每个片段的不同操作菜单项 - Different action menu items for each fragment with same navigation drawer in Android Android导航抽屉片段 - Android Navigation Drawer Fragment 如何在 android 显式深度链接中将安全参数作为捆绑包传递? - How to pass safe args as bundle in android explicit deep link? 如何在Android中将ArrayList从Activity传递到Fragment - How to Pass ArrayList from Activity to Fragment in Android 如何使用 java 将数据从片段传递到 android 中的另一个片段? - How to pass data from fragment to another fragment in android using java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM