RoboGuice:如何在我的自定义类中使用RoboGuice提供的注入?

时间:2014-07-08 06:09:15

标签: android dependency-injection android-contentresolver roboguice

我创建了一个依赖于ContentResolver的类:

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

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

    [...]
}

ContentResolver是RoboGuice提供的注入(https://github.com/roboguice/roboguice/wiki/RoboGuice-Standard-Injections),但这假设我在扩展RoboGuice类的RoboActivity类中使用注入。

有没有办法在我的自定义类中使用RoboGuice提供的注入?

1 个答案:

答案 0 :(得分:1)

将此行添加到MyClass的构造函数中:RoboGuice.getInjector(myApplicationContext).injectMembers(this)