Roboguice provides send and receive for events related to the context lifecycle, and events supported by the system defaults are:
Onactivityresultevent
Onconfigurationchangedevent
ONCONTENTC hangedevent
Oncontentviewavailableevent
Oncreateevent
Ondestroyev ent
Onnewintentevent
Onpauseevent
Onrestartevent
onresumeevent< p>onstartevent
Onstopevent
A simple example is as follows:
public class MyActivity extends Roboactivity {//You must ' register ' your listener in the ' current/context by Inje
Cting it.
The injection is commonly to the activity,//but can also being done anywhere really.
@Inject protected mylisteners mylisteners; }//In this example, all the listeners are in A//Mylisteners class, but really they could/is an
Ywhere as long as it ' s registered.
Can even put the listeners directly to//your activity classes if you like!
Class Mylisteners {//any method with the void return type and A//single parameter with @Observes annotation
Can is used as an event listener.
This is one listens to Onresume. public void Dosomethingonresume (@Observes onresumeevent onresume) {ln.d (' called Dosomethingonresume in Onre
Sume "); }//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 on
CreateEvent onCreate) {ln.d ("OnCreate savedinstancestate is%s", Oncreate.getsavedinstancestate ())}
Course, 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!") }
}
Notes on events are as follows:
Use @inject to define the Listener of events in the context.
An event can only be transmitted in a particular context (activity) and not sent across a context, accepted.
event, in addition to providing the context-related events listed above, can also use custom events.
@observes can be applied only to methods, not to constructors.
The following is a custom event myevent that is sent by observer this custom event to receive custom events.
Public
class Eventdemo extends Roboactivity {
@Inject protected eventmanager eventmanager;
@InjectView (R.id.button) button button;
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (R.layout.eventdemo);
Button.setonclicklistener (Mgolistener);
}
Private Onclicklistener Mgolistener = new Onclicklistener ()
{public
void OnClick (View v)
{
Eventmanager.fire (Eventdemo.this,new myevent ());
}
;
protected void Handleevent (@Observes myevent event) {
Toast.maketext (this, "Custom event",
toast.length_ LONG). Show ();
}
Class myevent{
//put any memeber your want here.
}
This example downloads: Http://www.imobilebbs.com/download/android/roboguice/EventDemo.zip
View a full set of articles: Http://www.bianceng.cn/OS/extra/201301/34950.htm