Android Roboguice Use Guide (19) Send Receive events

Source: Internet
Author: User
Tags event listener

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.