My favorites | Sign in
Project Home Wiki Issues Source
Search
for
Events  
Using Events in your RoboGuice application
Updated Mar 26, 2012 by mbur...@gmail.com

RoboGuice supports the ability to send and receive context-based events in your application. This can be particularly useful to help you modularize by allowing you to take much of your code out of the various onCreate(), onResume(), etc. lifecycle methods in Android.

Thanks to Adam Tybor and John Ericksen for making this feature happen.

A simple example:

public class MyActivity extends RoboActivity {
    // You must "register" your listener in the current context by injecting it.
    // Injection is commonly done here in the activity, but can also be done anywhere really.
    @Inject protected MyListeners myListeners;

}


// In this example, all the listeners are in a MyListeners class, but really they could
// be anywhere as long as it's registered.  You can even put the listeners directly into
// your activity classes if you like!
class MyListeners {
    
    // Any method with void return type and a single parameter with @Observes annotation
    // can be used as an event listener.  This one listens to onResume.    
    public void doSomethingOnResume( @Observes OnResumeEvent onResume ) {
        Ln.d("Called doSomethingOnResume in onResume");
    }
    
    // As you might expect, some events can have parameters.  The OnCreate event
    // has the savedInstanceState parameter that Android passes to onCreate(Bundle)
    public void doSomethingElseOnCreate( @Observes OnCreateEvent onCreate ) {
        Ln.d("onCreate savedInstanceState is %s", onCreate.getSavedInstanceState())
    }
    
    // And of course, you can have multiple listeners for a given event.
    // Note that ordering of listener execution is indeterminate!
    public void xxx( @Observes OnCreateEvent onCreate ) {
        Ln.d("Hello, world!")
    }
}

Things to be Aware of

  • You must register your listeners in a context by injecting them.
  • Events only propagate within a given context, not across contexts. So don't try to expect a listener registered in one activity to get the events sent by another activity.
  • RoboGuice includes many built-in events for common Android operations. But you may absolutely create new events unique to your application.
  • @Observes may only be used on methods, not constructors.

A Partial List of Included Events

  • roboguice.activity.event
    • OnActivityResultEvent
    • OnConfigurationChangedEvent
    • OnContentChangedEvent
    • OnContentViewAvailableEvent
    • OnCreateEvent
    • OnDestroyEvent
    • OnNewIntentEvent
    • OnPauseEvent
    • OnRestartEvent
    • OnResumeEvent
    • OnStartEvent
    • OnStopEvent

Creating your Own Events

Let's say you want to trigger a "Buy" event when a user clicks on a buy button.

public class MyOtherActivity extends RoboActivity {  
    // You'll need the EventManager if you want to trigger an event.  
    @Inject protected EventManager eventManager;
    
    @InjectView(R.id.buy_button) protected Button buyButton;
    
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
        
        buyButton.setOnClickListener( new OnClickListener() {
            public void onClick() {
                // trigger the event
                eventManager.fire(new MyBuyEvent() );
            }
        })
    }
    
    
    // handle the buy event
    protected void handleBuy( @Observes MyBuyEvent buyEvent ) {
        Toast.makeToast(this, "You won't regret it!", Toast.LENGTH_LONG).show();
    }
    
    
}


// The event class can be anything you want
class MyBuyEvent {
    ...
}

An "event" may be of any object type. So it's up to you what you might like to put in MyBuyEvent.

You Might Like...

Powered by Google Project Hosting