如何最有效地从一个片段传达一个信息给其父(相当于的onActivityResult) [英] How best to convey a message from one Fragment to its parent (onActivityResult equivalent)
问题描述
OK我知道,我可能会考虑这种错误的方式,但...
OK I know that I may be thinking about this the wrong way but ...
A)我有活动的装载片段F1,它加载片段F2。 F2执行应该导致其被关闭,然后F1基于它是如何关闭执行某项操作的操作。
A) I have activity A loading Fragment F1 which loads fragment F2. F2 performs an action which should result in it being closed and then F1 performing a certain action based on how it was closed.
B)现在,因为我还配套一个窗格,非片段,F1版还通过startActivityForResult调用加载F2间接的影响。这意味着,当F2的包装活动结束F1临危一个电话的onActivityResult与F1可以做它需要做的。
B) Now because I'm also supporting a single pane, non-fragment, version F1 also loads F2 indirectly via a startActivityForResult call. This means that when F2's wrapper activity is finished F1 recieves an onActivityResult call and F1 can do what it needs to do.
不过,我挣扎,看落实我想要的(A)的最佳方式。我已经F2回拨活性的弹出它关闭堆栈。如果我当时在看传递一个消息,F1做它需要做的?或者(我想F1可以负责弹出F2从堆栈中)。
However, I'm struggling to see the best way to implement what I want for (A). I have F2 calling back to activity A to pop it off of the stack. Should I then be looking at passing a message to F1 to do what it needs to do? Alternatively (I suppose F1 could be responsible for popping F2 off of the stack).
我不知道是我是否在正确的轨道上与问候的消息传递回来,就通过父活动或有被关闭的F1回应F2执行的东西,它需要一个更直接的方式与F1做它需要做的。
What I'm wondering is whether I'm on the right track with regards passing messages back and forwards via the parent activity or is there a more direct way of F1 responding to F2 performing something that requires it to be closed and F1 do what it needs to do.
在此先感谢。彼得。
推荐答案
如果您的片段是紧密联系在一起的,只是不单独活动的运行。对于单窗格情况下,你可以只是一个活动中切换片段。
If your fragments are closely tied together, just don't run them in separate activities. For the single pane case you can just switch fragments within one activity.
否则,如果你想将它们活动之间分开,你需要使用的onActivityResult()
型号为传播结果返回,并在双窗格中案通过只具有第一片段的第二个呼叫的onActivityResult()
模仿吧。需要注意的是 Fragment.setTargetFragment()
包括请求code参数,以方便这一点。
Otherwise, if you do want to separate them between activities, you need to use the onActivityResult()
model for propagating results back, and in the dual-pane case "emulate" it by just having the second call onActivityResult()
of the first fragment. Note that Fragment.setTargetFragment()
includes a request code argument to facilitate this.
这篇关于如何最有效地从一个片段传达一个信息给其父(相当于的onActivityResult)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!