[英]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.