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.
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 callonActivityResult()of the first fragment. Note thatFragment.setTargetFragment()includes a request code argument to facilitate this.