Search code examples
androiddependency-injectionandroid-contentresolverroboguice

RoboGuice: how to use RoboGuice-provided injections in my custom class?


I've created a class that depends on a ContentResolver:

public class MyClass
{
    // these Injects won't work
    @Inject
    private ContentResolver m_contentResolver;

    @Inject
    public MyClass( ContentResolver resolver )
    {
        m_contentResolver = resolver;
    }

    [...]
}

ContentResolver is a RoboGuice-provided injection (https://github.com/roboguice/roboguice/wiki/RoboGuice-Standard-Injections), but this assumes that I am using the injection in a class that extends a RoboGuice class such as RoboActivity.

Is there a way to use a RoboGuice-provided injection in my custom class?


Solution

  • Add this line to the constructor of MyClass: RoboGuice.getInjector(myApplicationContext).injectMembers(this)