简体   繁体   English

将数据从一个活动返回到另一个活动的片段?

[英]Returning data from an activity to another activity's fragment?

How can I pass data between an activity and a fragment belonging to another activity? 如何在活动和属于另一个活动的片段之间传递数据? I think that it should be something similar to passing data between activities through intents, but I'm not able to figure it out and couldn't get any search result on it either. 我认为这应该类似于通过意图在活动之间传递数据,但是我无法弄清楚它,也无法获得任何搜索结果。

For example, I have two activities A and B. 例如,我有两个活动A和B。

A has 3 fragments - "x", "y" and "z". A具有3个片段-“ x”,“ y”和“ z”。 Now I call B from "x" using startActivityForResult. 现在,我使用startActivityForResult从“ x”调用B。

When I try and come back to "x" from "B" via setResult, it actually takes me to onActivityResult of "A" and not the one in "x...". 当我尝试通过setResult从“ B”返回“ x”时,实际上将我带到“ A”的onActivityResult而不是“ x ...”中的那个。 Plus the requestCode and resultCode are some number (65536) and -1 respectively. 加上requestCode和resultCode分别是某个数字(65536)和-1。

Can anyone please help me with this? 谁能帮我这个忙吗?

Most likely your parent activity inplements onActivityResult() too, but in that method you forgot to call super.onActivityResult(requestCode, resultCode, intent); 您的父级活动很可能也增加了onActivityResult() ,但是在该方法中,您忘记了调用super.onActivityResult(requestCode, resultCode, intent); to pass unhandled request codes. 传递未处理的请求代码。 Or you got request code clash and the same code is expeceted by parent activity and the fragment. 否则,您将获得请求代码冲突,并且父活动和该片段将使用相同的代码。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM