如何在Android中使用RoboGuice注入上下文?

ira*_*hil 3 android dependency-injection roboguice

我想将我的上下文注入我的实用工具类,我已经看到使用静态字段的示例,有没有办法用静态字段来做?

el-*_*ano 7

我倾向于在需要时使用Provider来注入上下文.

public class MyClass
{
    private Provider<Context> contextProvider;

    @Inject
    public MyClass(Provider<Context> contextProvider)
    {
        this.contextProvider = contextProvider;
    }

    public doSomething()
    {
        Context c = contextProvider.get();
    }
}
Run Code Online (Sandbox Code Playgroud)