3

How can I get my activity to update a fragment it controls? It goes like this, I have a DatePickerDialog, that when your in the Fragment its called. I have a call back to the activity to let it know when this datepickerdialog is done having a date selected that passes back the date selected. So my activity has it, but I want the Fragment that called the DialogFragment to have access to it, instead of the activity above. Is this possible?

So I have this EditSessionActivity, that in it calls the SessionEdit Fragment. Which loads the fragment with UI and a date button, when that is clicked a DialogFragment called DatePickerDialogFragment is launched, this works great, and calls back to the EditSessionActivity that implements the OnDialogDoneListner that I wrote. The data is there, but I want to push that data back to the SessionEdit fragment to update its UI. How would I go about doing this?

Earlier today I thought I was going to easily get this to work, tried to answer something here:

How to create datePicker and timePicker dialogs in fragment class?

realizing that my UpdateDisplay (From the link above) there in the above link does zilch for updating the buttons text from the calling fragment like I want.

Community
  • 1
  • 1
Codejoy
  • 3,722
  • 13
  • 59
  • 99

2 Answers2

9

A Fragment is going to need to communicate with its parent Activity and vice versa. Define an interface for both to conform to and allow appropriate listeners to subscribe to events. For example, I would say that any time a fragment wants to change the UI outside itself, this should be delegate to the Activity. One Fragment shouldn't know or care if other Fragments exist. (To be comical, if the Fragment wants a sandwich it should call 'sudo make me a sandwich')

Let's say this is an email client and the Fragment wants the user to select an attachment. It should call something like MyParentActivity.selectAttachment(AttachmentSelectedCallback). If this is a tablet, maybe the Activity invokes a new fragment, leaving the compose fragment visible. If this is a phone, maybe a new Activity is started or the existing Fragment is swapped out for the attachment selector.

One way I'm thinking about an Activity/Fragment relationship is the way microkernels are designed. The Activity is the kernel, the Fragment is a service. The kernel manages communication and spinning up or shutting down the services, but generally speaking services shouldn't be directly aware of one another. Note there are performance penalties to this design that are not always worth the tradeoff.

cyngus
  • 1,763
  • 13
  • 14
  • well that sounds fair enough. I think there is some issue with my way of getting to the fragment reference. I sorta spoke of it here: http://stackoverflow.com/questions/6729680/getting-data-to-my-fragment-from-my-activity i dunno, so if I want to have the activity talk to a fragment, i just implement a listener in the fragment? (not sure if my terms are correct) – Codejoy Jul 20 '11 at 01:56
  • Yeah, you can define whatever interface you want, seems like you want the Fragment to call some method in the Activity, passing some sort of callback as an argument. This callback object will probably contain a reference to the Fragment. – cyngus Jul 20 '11 at 05:12
  • ahh hopping between questions sorry. So once that call back is sent back to activity thats how i get at the data in the fragment, and if it has a reference and say my date button is public, i can just do the .setText on it there and update it with the dialogfragments date picked. etc... – Codejoy Jul 20 '11 at 07:45
  • Or better yet, don't give it access to your Fragment, just give it a reference to the UI element that needs to be update. Narrow interfaces are better than wider ones. – cyngus Jul 20 '11 at 15:32
  • Ahh so the callback contains a reference just to my ui elements? – Codejoy Jul 20 '11 at 15:35
  • It could, I mean, its up to you to define, but generally speaking you want to allow one piece of code as narrow of a window as possible into another piece of code. – cyngus Jul 20 '11 at 17:02
  • sounds fair. Now with all this fragment back and forth, I decided I wanted to redesign the UI to have two "panes" again, splitting up this fragment into two. So now i'm scratching my head on how this will go, it will be another question for sure :) thanks though I did get the fragments talk to eachother how I wanted with listeners! works well. – Codejoy Jul 20 '11 at 19:31
  • Not a great idea for an Activity to have knowledge of the existence of any specific UI elements (aka Views) that exist within a Fragment. It breaks encapsulation and introduces an unnecessary dependency on an implementation detail. – Julian A. Jan 22 '12 at 08:55
1

If you see the TitleFragment example in this page : http://developer.android.com/guide/topics/fundamentals/fragments.html

You will see a new instance of the DetailsFragment is created everytime a new index in the TitleFragment is created instead of updating the existing UI itself. You might want to create a new instance of the fragment every time you want to change the view.

500865
  • 6,920
  • 7
  • 44
  • 87
  • The one thing you can try is to override the `onResume` method in the fragment to update views by getting the data from the activity. – 500865 Jul 16 '11 at 21:20
  • Do you mean the part that's in the ShowDetails()? and I think my biggest issue was funneling the data between the fragments, i.e. how to set what date was picked, not just displaying it or refreshing it after it was picked. – Codejoy Jul 17 '11 at 04:51