0

I'm using RoboGuice in my Android application, I'm not sure how to pass object between Activity to Fragment?

I am aware about an approach where in you can serialize and pass in the bundle. In this article How to pass data between fragments in one of the comments it is mentioned

If you use Roboguice you can use the EventManager in Roboguice to pass data around without using the Activity as an interface. This is quite clean IMO.

I'm not sure how to do that, can anyone give an example?

Community
  • 1
  • 1
Mayank Mehta
  • 689
  • 1
  • 8
  • 12

2 Answers2

0

Since Fragments are contained within an Activity, and you normally have a reference to it, it's pretty common for a Fragment to expose either a specific constructor or setter methods that the Activity can invoke. There's no need for serialization. To got the other way (Fragment -> Activity) the Fragment should expose an interface that the Activity can listen for events on.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • No, it has to work the other around : the fragment has to pull a listener from the interface. Fragments have their own lifecycle and the activity doesn't know about it. An activity can't guarantee to set a listener each time an instance of fragment is created. – Snicolas Apr 21 '13 at 08:04
0

I don't think that approach is unique to roboguice.

I prefer event propagation as opposed to the (perfectly acceptable) callback approach outlined by dmon. I have provided a mini-guide here...I suspect roboguice's version is very similar. 2 components (activity + fragment), fragment broadcasts a 'I did this' message whilst the activity declares 'I'm listening for that'...

Community
  • 1
  • 1
BrantApps
  • 6,362
  • 2
  • 27
  • 60