Android Roboguice Usage Guide (inject) Extra

Source: Internet
Author: User

When using intent to start a activity,service, the data can be transmitted through the Putextra, and the triggered Activity,service can use Getintent Getextras () Extra, The corresponding parameter values are then based on the extra key value (key).

Roboguice provides a neat way to get these extra values through @injectextra tags.

This example uses two Activity,injectextrademo to trigger the injectextrareceiver, and when intent is created in Injectextrademo, two parameters are placed Putextra .

public class Injectextrademo extends 

roboactivity {          
 @InjectView (R.id.button) button button;     
 @Inject context;     
          
 @Override public 
 void OnCreate (Bundle savedinstancestate) {     
 super.oncreate (savedinstancestate);     
 Setcontentview (R.layout.injectextra);     
 Button.setonclicklistener (Mgolistener);     
 }     
          
 Private Onclicklistener Mgolistener = new Onclicklistener ()     
 {public     
 void OnClick (View v)     
 {     
 Intent DI = new Intent ();     
 Di.setclass (context, injectextrareceiver.class);     
 Di.addflags (Intent.flag_activity_new_task     
 | Intent.flag_activity_single_top);     
 Di.putextra ("Extra1", "Message1");     
 Di.putextra ("Extra2", "Message2");     
 Context.startactivity (DI);     
 }     
 ;     
          
}

In Injectextrareceiver through the @injectextra tag, Roboguice automatically injects the values passed by intent for these variables, optional=true indicates that the extra is optional, if the incoming Intent does not contain this extra, the value is null.

public class Injectextrareceiver extends roboactivity{     
          
 @InjectView (r.id.textview) TextView TextView;     
 @InjectExtra ("Extra1") String extra1;     
 @InjectExtra ("Extra2") String Extra2;     
 @InjectExtra (value= "Extra3", optional=true) String extra3;     
          
 @Override public 
 void OnCreate (Bundle savedinstancestate) {     
 super.oncreate (savedinstancestate);     
          
 Setcontentview (r.layout.injectextrareceiver);     
          
 Textview.settext ("Extra1:" +extra1     
 + "\r\nextra2:" +extra2     
 + "\R\NEXTRA3:" +extra3);     
          
 }     
          

This example downloads: Http://www.imobilebbs.com/download/android/roboguice/InjectExtraDemo.zi p

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.