Monday, 15 August 2011

android - roboguice how to inject custom class -


Hi I am currently using roboguice as we know, we use encapsulation to inject in class As in

 @InjectView (R.id.list) ListView   

@ Injection Nation works because I am a robo activity or a robo class

My question is if I want to inject a custom class, which

  public class CustomUtilManager {} < / Code>  

, then I want to inject it into Robo Activity

My second question is if I have a class, then this is not something Also sub-class of Robo Class

  MyOwnClass {} of public class   

How can I get the injector and inject the injection class I , Such as custom eutilizer

that is how I can say

  public class MyOwnClass {@Inject CustomUtilManager customUti LManager; }    

injection of custom class in Robo Activity

Inject a custom class by using the @ Injects annotation, but the injected square must one complete the following conditions:

    < Li> Custom class is a default constructor (with no arguments)
  • In the custom class is a injection maker
  • Handles (more complex)

    The easiest way is to use a default constructor explicitly if your constructor requires logic, then it should be injected:

      public class custom class {@Injected public CustomClass (reference references, other others) {...}}   

    @ injection on constructor Notice the annotation. The square of each argument is also injected by robogues. Should be. RoboGuice (for example, context ) is provided outside of many injection boxes for Android classes.

    Injecting inside a custom class

    If you create an example of your custom class with robogues (for example @Inject Annotation), all the fields marked with @ injected will be automatically injected.

    If you want to use the new custom class () , then you have to inject yourself:

      public class custom class {@ Injection other others; Phu Fu; Public Custom Class (Reference Reference) {Final RoboInjector Injector = RoboGuice.getInjector (reference); // injector.injectMembersWithoutViews (this) will inject all the fields marked with @ Inject annotation; // This will make an example of Fu Fu = Injector.Get Instance (Fu Class); }}   

    Note that you must pass your constructor to context to get the injector.

No comments:

Post a Comment