Android Roboguice Usage Guide (inject context)

Source: Internet
Author: User
Tags constructor

In Android applications, many places need to refer to context objects (activity, application,service, etc.). Roboguice makes referencing the context object very easy.

See the following example, where you define a class contextinfo that is not in the activity, and you need to reference the context object:

Class contextinfo{     
          
 final context context;
 @Inject 
 ContextInfo (context context) {     
 This.context=context     
 }     
          
 String Getpackagename () {return     
 context.getapplicationinfo (). PackageName;     
 }     
}

When a context object needs to be applied, the @inject tag is used, and roboguice automatically injects the required context object.

Define a Injectcontextdemo, using a textview to display the getpackagename content of the ContextInfo.

public class 

Injectcontextdemo extends Roboactivity {     
          
 @InjectView (r.id.textview) TextView TextView;     
 @Inject ContextInfo ContextInfo;     
          
 @Override public 
 void OnCreate (Bundle savedinstancestate) {     
 super.oncreate (savedinstancestate);     
          
 Setcontentview (r.layout.injectcontext);     
 Textview.settext (Contextinfo.getpackagename ());     
          
 }     
          

A injectcontextdemo is defined in Injectcontextdemo, and an instance of it is automatically created using the @inject notification Roboguice. Roboguice when this object is created, it invokes its injectable constructor (see Android Roboguice Use guide: Just-in-time bindings) to automatically pass in the context object.

If you need to apply the Application object, you can change the constructor to

@Inject 
 ContextInfo (roboguicedemoapplication context) {     
 This.context=context     
}

or reference activity

@Inject 
 ContextInfo (activity context) {     
  This.context=context     
}

This example downloads: Http://www.imobilebbs.com/download/android/roboguice/InjectContextDemo. 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.