活动图和非依赖发现

活动图和非依赖发现

本文介绍了活动图和非依赖发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始用匕首,喜欢它pretty多,但现在面临一些困难。我的情况如下:有一个活动,并为它的依赖。依赖被注入到活性,并要求该活动的参考。就像这样:

I'm starting using the dagger, like it pretty much, but now facing some difficulties. My scenario is as follows: there's an activity and a dependency for it. Dependency is injected to the activity, and requires a reference to that activity. Just like this:

public class MainActivity extends BaseActivity {
    @Inject ScbeHelper scbeHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
    }
}

public class ScbeHelper {

    protected static final String TAG = "scbe_helper";
    private BaseActivity activityContext;

    @Inject
    public ScbeHelper(BaseActivity context) {
        this.activityContext = context;
    }
}

我在下面的从github上对活动的图表。所以我创建了我的项目类似的结构。首先, BaseActivity 类,从 MainActivity 继承:

I'm following dagger's example from the github for activity's graphs. So I've created a similar structure in my project. First, the BaseActivity class, from which MainActivity is inherited:

public abstract class BaseActivity extends Activity {
    private ObjectGraph activityGraph;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        protoApp application = (protoApp) getApplication();
        // Exception happens in next line, inside plus() method
        activityGraph = application.getApplicationGraph().plus(getModules().toArray());

        // Inject ourselves so subclasses will have dependencies fulfilled when this method returns.
        activityGraph.inject(this);

        ((protoApp)getApplication()).inject(this);
    }

    protected List<Object> getModules() {
        return Arrays.<Object>asList(new ActivityModule(this));
    }

    public void inject(Object object) {
        activityGraph.inject(object);
    }
}

和模块:

@Module(injects={MainActivity.class})
public class ActivityModule {
    private final BaseActivity activity;

    public ActivityModule(BaseActivity activity) {
        this.activity = activity;
    }

    @Provides @Singleton BaseActivity provideActivity() {
        return activity;
    }
}

现在的问题: 上com.example.proto.BaseActivity没有注射成员。你想添加一个注射构造?公共com.example.proto.ScbeHelper(com.example.proto.BaseActivity)

在换句话说,供应商的方法 ActivityModule.provideActivity()并没有真正提供 BaseActivity 的实例由于某些原因,尽管在我的理解它的正确设置。有谁看到我的设置错误?我失去了在匕首的逻辑的东西吗?

In other words, provider method ActivityModule.provideActivity() doesn't really provide the instance of BaseActivity for some reason, though in my understanding it's set up correctly. Does anyone see an error in my setup? Am I missing something in dagger's logic?

在此先感谢!

推荐答案

我不是匕首专家,但是你有2个问题:

I'm no Dagger expert, but you have 2 issues:

下面是我做得到它的工作:

Here's what I did to get it to work:

@Provides @Singleton ScbeHelper provideScbeHelper() {
    return new ScbeHelper(activity);
}

这样做是提供您与ScbeHelper它需要的上下文,但只剩下1注解驱动的注入也使匕首可以解决这个问题。希望这有助于。

What this does is it provides your ScbeHelper with the context it needs, but leaves only 1 annotation-driven injection so dagger can resolve it. Hope this helps.

这篇关于活动图和非依赖发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!