roboguice/roboguice — pull request #28 by stephanenicolas (2013-05-08 21:19:35+00:00)
Title: Sticky events and Producers + astroboy as a a demo of robofragments and roboguice events

I just hacked RoboGuice and could achieve something that might interest you as a contribution :

1. change astroboy FightForcesOfEvilActivity to a fragment. In portrait, the fragment is loaded in a separate activity. In landscape, the MasterConsoleActivity is split in two vertically and you can see the fragment with the buttons simultaneously. All buttons will remove the fragment, except the "fight forces of evil" button that will display the fragment.
2. add support for some event : the fragment fires an event and the console activity observes it (using roboguice event manager mechanism).
3. support for an event the other way around : the activity fires an event and the fragment observes it.

This example was developed to illustrate current roboguice features and, notably, events and fragments supports.

Nevertheless, it also pointed that, with respect to fragment life cycle, a fragment is only aware of events that occurs AFTER it is created. Thus, it is difficult for newly created fragments to fully reflect the state of the activity if an event has been triggered before they were created.

As a workaround, I contributed 2 different mechanisms :

1. a new EventProducer interface : you can register an event producer to the event manager bus. Everytime a new listener is registered to the bus, the bus asks to a potential producer to produce an event that is fired to the new listener.
You would typically implement producers as inner classes of activities and register them dynamically.
2. a sticky event annotation : an event class can be annotated as sticky : in that case, the last instance of event of that class that is fired by the event manager will be kept in a map (class of event <-->event). The last event will be fired to a newly registered listener.
This one is more handy and convenient than the previous mechanism.

Sorry for the long mail, I hope it might interest you, the idea is really to mimic something that dagger has and makes it much easier to communicate between activities and fragments with respect to their life cycles.

PS : I would like to create an annotation for producers, but I am not there yet.