Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, there are three main ways to get injection in your objects when using RoboGuice.</p> <ol> <li><p>Call RoboGuice.injectMembers() yourself for the object you want injection to be performed on. For most POJOs this would likely be done in the object's constructor, but could also be done at other times. Note that this is a perfectly solution but is probably the least desirable of the options as it hardcodes a dependency on your DI framework (RoboGuice).</p></li> <li><p>Inject into a class that already does #1 for you. You're already familiar with this method... this is exactly what RoboActivity does. Basically, most of what RoboActivity does is just call RoboGuice.injectMembers for you.</p></li> <li><p>Inject TelephonyManager into a class that was itself injected. This is usually the most desirable, although in your particular case it may not be desirable. Let me explain this below:</p> <pre><code>class MyActivity extends RoboActivity { @Inject PhoneNumber phone; ... } class PhoneNumber { @Inject TelephonyManager tm; } </code></pre> <p>Basically, RoboGuice just needs a chance to perform injection on your instance. For normal POJOs that are not created by RoboGuice, there's no opportunity for RoboGuice to work its magic unless you call injectMembers directly, hence solution #1. However, if you allow RoboGuice to instantiate an object for you, eg. by injecting it, then RoboGuice has control over the creation of the object and can do injection for you in that object. Thus, in the example above, when RoboGuice creates a new PhoneNumber and puts it in the <code>phone</code> variable, RoboGuice will perform injection on the instance and <code>tm</code> will be injected properly. Obviously, if you construct PhoneNumber using the new() operator instead of letting RoboGuice do it, RoboGuice won't have an opportunity to perform injection and tm will be null.</p></li> </ol> <p>If #3 works for you, then I would go for it. It's the most flexible way to do things, and it encourages good practices by letting the DI framework instantiate your objects for you.</p> <p>However, if a PhoneNumber is not something that you typically let RoboGuice instantiate for you, then you may want to consider going with option #1.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload