How best to convey a message from one Fragment to its parent (onActivityResult equivalent)

跟風遠走 提交于 2019-12-22 10:34:38

问题


OK I know that I may be thinking about this the wrong way but ...

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) 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.

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).

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.

Thanks in advance. Peter.


回答1:


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.

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.




回答2:


I have activity A loading Fragment F1 which loads fragment F2

IMHO, fragments should not load other fragments. Activities load fragments, based upon available screen space. Fragments should neither know nor care whether any other fragment exists in the current activity, or if other fragments are in other activities.

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?

Yes.

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.

I would not have F1 even know that F2 exists, or vice versa. When the user does something in F1 that should result in a major context shift (e.g., display some other fragment/activity), F1 should let the hosting activity know, perhaps via a listener interface registered with F1 (to support multiple possible hosting activities). The activity would then arrange for F2 to appear, either in its own activity or in another activity. Similarly, when F2 wraps up, it would let its hosting activity know via a listener interface, and that activity can route control back to the appropriate spot.

I am somewhat skeptical of your whole "F2 performs an action which should result in it being closed" approach, unless F2 is a DialogFragment.



来源:https://stackoverflow.com/questions/5996572/how-best-to-convey-a-message-from-one-fragment-to-its-parent-onactivityresult-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!